我想問我如何用基本代碼使我的程式回圈,(請不要推薦神秘的),例如如果用戶輸入 1 它會重新運行程式,如果他們輸入任何其他內容,程式就會結束所以請幫幫我在這個問題中。
uj5u.com熱心網友回復:
你有兩個選擇:
迭代
使用無限while True
回圈并在用戶輸入時中斷1
while True:
inp = input("Enter a number: ")
if inp == '1':
break
# some code goes here
遞回
創建一個遞回函式,如果用戶輸入則回傳1
def get_input():
inp = input("Enter a number: ")
if inp == '1':
return
# some code goes here
get_input()
get_input()
uj5u.com熱心網友回復:
使用while True
回圈并在滿足特定條件時退出它:
while True:
if int(input("Enter a number: ")) == 1: # if user's input is '1'
break # break out of the infinite loop
# do repetitive stuff here ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506619.html
上一篇:在vscode中關閉一個檔案夾