我的任務是製作這個程序。一個讀取tests.txt
的程序會顯示所有分數以及分數的平均值。該程序還必須使用循環。在python中掙扎的項目
這是我到目前爲止有:
def main():
scorefile = open('test.txt', 'r')
test1 = scorefile.readline()
test2 = scorefile.readline()
test3 = scorefile.readline()
test4 = scorefile.readline()
test5 = scorefile.readline()
scorefile.close()
print(test1, test2, test3, test4, test5)
total = (test1 + test2 + test3+ test4 + test5)/5.0
print('The average test score is:', total)
main()
我寫信給test.txt
文件已經與這些數字:
95
87
79
91
86
歡迎的StackOverflow!請務必詢問實際問題,以便我們明確瞭解我們如何爲您提供幫助。 – 2014-10-04 02:57:43
默認情況下,這些值以字符串形式存儲在本地變量中。你必須將它們轉換爲浮點數或整數才能工作。 – karthikr 2014-10-04 02:59:31