How can I write to the textfile with “while”?

后端 未结 2 1671
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 09:21
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

相关标签:
2条回答
  • 2021-01-17 09:52

    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 讨论(0)
  • 2021-01-17 10:01

    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()
    
    0 讨论(0)
提交回复
热议问题