2017-03-02 8 views
0

このプログラムでは、空のテキストファイルにエントリを追加しようとしています。以下は私のコードです:プログラムが正しくビルドをWhileループを使用してPythonでファイルに追加/追加する

###Adding to an Empty File 
filename = 'guest_book.txt' 

message = input("Please enter your name for our records: ") # retrieving input 

while message != 'finished': # checking for value that will end the program 
    with open(filename, 'a') as f: 
     f.write(message) 

私は入力の名前を一度、しかし、何も起こりませんし、テキストファイルは空のまま。何か案は?

答えて

0

メッセージを一度要求した後、メッセージを探すループを開始します(finished)。しかし、初めてmessageという何か別のものを入力した場合、この条件は決して真実になりません。

###Adding to an Empty File 
filename = 'guest_book.txt' 

while True: # checking for value that will end the program 
    message = input("Please enter your name for our records: ") # retrieving input 
    if message == 'finished': 
     break 
    with open(filename, 'a') as f: 
     f.write(message) 
+0

私の出力はので、私はいくつかの追加ストリップ()メソッドを持っていたまでごちゃ混ぜにした

は、私はあなたがしたいと思います。そうでなければ、これはうまくいったありがとうございました! –

関連する問題