我創建了一個讀取.txt文件的程序,並在其中附加了一個等級列表。當我嘗試從該列表中獲取最小值和最大值時,即使列表包含大量數字,它也會返回相同的值。'min'和'max'函數從數字的.txt文件返回相同的值
.txt文件的格式是這樣的:
name
grade
name
grade
這裏是我的代碼:
def main():
#open gradebook
gradebook_file = open ('gradebook.txt', 'r')
name = gradebook_file.readline()
grade = float (gradebook_file.readline())
numbers = []
for lines in gradebook_file:
numbers.append (grade)
_min = min (numbers)
_max = max (numbers)
#define variable to read contents of gradebook
contents = gradebook_file.read()
#print contents
print (contents)
print ('The minimum average grade was a(n) ' + str (_min) + '.')
print ('The maximum average grade was a(n) ' + str (_max) + '.')
#close the gradebook
gradebook_file.close()
#call main
main()
在你長達一小時的調試會話中,你有沒有嘗試過'print(numbers)'? –
你正在閱讀的只有1個等級和1個名字,我正在修改你的代碼。 – Ambitions