![](/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.