2014-07-10 154 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