2014-07-10 82 views
0

我正在写一个简单的程序,我的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. 
>>> 

回答

0

Python的关心缩进:你的循环内的唯一的事情就是input语句,所以你write只会发生一次:已进入最后的值之后。

您需要缩进write语句与input相同,以便将其放入循环中。

+0

非常好!谢谢! – user3818550