我的任务是制作这个程序。一个读取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