![](/img/trans.png)
[英]Continue statement in Python only goes back at start of the loop. How to make it go in any place of code?
[英]How to make this code go back the start of the problemstate?
我正在嘗試創建一個程序,您可以在其中選擇 3 個選項。 復習、備忘單和數學方程式,但不要擔心。 有沒有辦法將“def r()”循環到“problemstate()”到開始,同時保存用戶在“inputr”中寫的東西。 本質上,我試圖在輸入器中輸入內容,保存用戶在那里寫的任何內容,然后輸入一個字母或其他內容以回到開頭。 並一遍又一遍地重復這個過程。 無需終止。 這是代碼的鏈接,謝謝。 https://trinket.io/python3/6e76fbb2f1代碼圖像以防您看不到飾品。
你很接近,只是有點偏離 def 正在做什么。 你想要的是首先定義你的 4 個函數( problemstate()
、 r()
、 c()
和m()
)。 然后有一個看起來像這樣的循環:
while (some_condition):
ps = problemstate()
if ps== 'R':
r()
# etc...
所以讓你的problemstate()
函數返回輸入。 甚至更好的是,您根本不需要problemstate()
:
while (some_condition):
print("What would you like to use this program for?")
print("(R)eviewing, (C)heat sheet, or (M)ath?")
ps = input("Please select the letter appropriately? ")
if ps == 'R':
r()
# etc...
您只需要添加一個無限循環,有很多不同的方法可以做到這一點(在我的示例中,我使用while True
)使用continue
重復該過程。
def problemstate():
while True:
# HERE: It is possible add a function to clean the console if you want
print("What would you like to use this program for?")
print("(R)eviewing, (C)heat sheet, or (M)ath?")
ps = input("Please select the letter appropriately? ")
if ps not in ["R", "C", "M"]:
# Perhaps show an error message and not continue
continue
elif ps == "R":
# function of r
elif ps == "C":
# ...
# End of loop
input_learning = input("Type all of the learning and knowledge you would like to use: ")
inputr = input("\nType 'p' if you want to refer back to the start and pick a different sheet or any other letter to exit: ")
if inputr == 'p':
continue
else:
raise SystemExit # break
我添加了一些條件來查看所選選項,即使您可以在ps
無效時顯示消息錯誤。
另外,我添加了一個新變量來保存學到的知識(例如,您可以將其寫入文件中)和inputr
只是為了輸入用戶想要做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.