本文介绍了我如何用“while”写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 run 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()
这篇关于我如何用“while”写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!