每一行代表一名學生,由學生人數,姓名,分段代碼和中期成績組成,全部由空白分隔。 第一個參數已經完成,該文件是開放的,
第二個參數是一款代碼 這是鏈接http://www.cdf.toronto.edu/~csc108h/fall/exercises/e3/grade_file.txt返回各自講座中的平均分數
我的代碼:
def average_by_section(the_file, section_code):
'''(io.TextIOWrapper, str) -> float
Return the average midtermmark for all students in that section
'''
score = 0
n = 0
for element in the_file:
line = element.split()
if section_code == line[-2]:
mark = mark + float(line[-1])
n += 1
lecture_avg = mark/n
return lecture_avg
我發現了一個指數出來的範圍。它是否正確?或者我只是打開錯誤的文件?
有人可以測試此代碼並下載該文件?我很確定它應該工作,但不適合我。
'score'應該是'mark'(反之亦然)? – RocketDonkey