![](/img/trans.png)
[英]How to Exit/Break Python Automation Script in If Statement (Not inside a loop)
[英]How to use an if statement to break a while loop when the word exit is typed?
我想让它在输入“退出”这个词时循环中断并打印一条消息。 你怎么做到这一点?
这是我到目前为止的代码:
file=open("C:/Users/wl/Documents/devices.txt","a")
while True:
newItem = input('Input the new device:')
if newItem == 'exit':
我试过在最后一行之后休息一下,但它不起作用,所以我假设我做错了什么。
尝试这个:
newItem = str()
while newItem != "exit":
newItem = input('Input the new device:')
print("'exit' was typed.")
这就是你如何使用break
语句实现你想要的:
while True:
newItem = input('Input the new device:')
if newItem.lower() == 'exit':
print('your message')
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.