开发者

How can I write to the textfile with "while"?

开发者 https://www.devze.com 2022-12-19 14:33 出处:网络
while 1: text_file = open(\"write_it.txt\", \"w\") word = input(\"Please add to a text file: \") What else do I need to add to make my code ru开发者_StackOverflown properly?This should work:
while 1:
    text_file = open("write_it.txt", "w")
    word = input("Please add to a text file: ")

What else do I need to add to make my code ru开发者_StackOverflown properly?


This should work:

text_file = open("write_it.txt", "w")
while 1:
    word = input("Please add to a text file: ")
    if not word:
        break
    text_file.write(word)
text_file.close()


Not sure, but that open statement inside of the while couldn't be affecting its behaviour? Have you tried just moving it out of while loop?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号