我正在嘗試創建一個程式,您可以在其中選擇 3 個選項。復習、備忘單和數學方程式,但不要擔心。有沒有辦法將“def r()”回圈到“problemstate()”到開始,同時保存用戶在“inputr”中寫的東西。本質上,我試圖在輸入器中輸入內容,保存用戶在那里寫的任何內容,然后輸入一個字母或其他內容以回到開頭。并一遍又一遍地重復這個程序。無需終止。這是代碼的鏈接,謝謝。 https://trinket.io/python3/6e76fbb2f1 代碼影像以防您看不到飾品。
uj5u.com熱心網友回復:
您只需要添加一個無限回圈,有很多不同的方法可以做到這一點(在我的示例中,我使用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僅輸入用戶想要執行的操作。
uj5u.com熱心網友回復:
你很接近,只是有點偏離 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...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477848.html
上一篇:在python中測量具有相同索引的二維串列元素的平均值
下一篇:何在R中運行分層自舉線性回歸?
