我正在写一个简单的程序,我的for循环有点不正常。它只读取最后一个条目。我知道它只是输出最后一个条目,我知道为什么,但我似乎无法修复它。 任何帮助都会很好。我可以让它以另一种方式运行,但我想使用循环
def main():
testfile = open('tests.txt', 'w')
#### THIS RUNS -- but only output is last entry #####
for count in range(1, 6):
test = int(input('Enter the test score: #' + \
str(count) + ' % '))
testfile.write(str(test) + '\n')
testfile.close()
print('The scores are in the tests.txt file.')
main()
将输出转换为文本文件
>>>
Enter the test score: #1 % 89
Enter the test score: #2 % 98
Enter the test score: #3 % 69
Enter the test score: #4 % 36
Enter the test score: #5 % 36
The scores are in the tests.txt file.
>>>
Python关心缩进:循环中唯一的东西是
input
语句,因此write
只发生一次:在输入最后一个值之后您需要将
write
语句缩进到与input
语句相同的位置,以将其放入循环中相关问题 更多 >
编程相关推荐