[英]While loop with break not working -> PYTHON
你们能帮我解决这个问题吗? IDK 为什么,但是当我键入 CLOSE 时它不起作用。
我已经解决了另一种运行相同代码的方法,但我想知道为什么这不起作用
谢谢
file = open("testing.txt",'+a')
while True:
line = input("enter something: ")
for i in line:
if i == "CLOSE" :
file.close()
break
else:
file.write(line + "\n" )
您使用内部for循环使这变得过于复杂。 您只需要:
with open('/tmp/foo.txt', 'a+') as f:
while (ui := input('Enter something: ')) != 'CLOSE':
print(ui, file=f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.