【发布时间】:2014-11-13 13:14:23
【问题描述】:
所以之前我创建了一个代码,提示用户输入 5 个不同的测试分数,然后将该列表保存到 test.txt。代码效果很好! 这是我的代码:
scorefile=open('test.txt','w')
for count in range(5):
print('Please enter test scores')
score=input('Test score:')
scorefile.write(str(score)+'%' + '\n')
scorefile.close()
但是现在,我遇到了一个问题。我有读取文件的代码。效果很好!但是当我尝试从列表中获取平均值时,我得到的只是 0.0。我一直在阅读我关于 python 的书来弄清楚如何使这项工作,但我现在严重卡住了。帮助? 这是我的代码:
scorefile=open('test.txt', 'r')
for line in scorefile:
print(line, end='')
score=scorefile
average = sum(score) / 5
print('The test average is', average)
scorefile.close()
【问题讨论】:
-
请将您的示例代码放入代码块中。为此,请选择您的代码并按工具栏中的
{}按钮。