[英]How to get my program to reset and add a continue prompt that will restart automatically restart program
我創建了一個單重最大功率提升計算器。 一切運行正常,但我希望它以重新啟動提示結束,以防用戶想要計算另一個升程。 (請記住,我是編碼和Python的新手)。
我玩過不同的變量,但是我在正確地聲明函數然后運行循環方面苦苦掙扎。
Welcome= 'Hello and welcome to one rep max calculator, Please follow the prompt below'
print (Welcome)
reps= int(input('Rep Count?'))
initial_weight= int(input('Weight lifted?'))
bench_max=(.0333 * reps + 1) * initial_weight
print (bench_max)
reply= 'y'
question= 'Do you want another calculation?'
def yes_or_no(reply):
reply= 'y'
while "the answer is invalid":
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
yes_or_no(reply)
沒有錯誤消息,但是無論我按是還是否,程序都不會重新啟動。
這將提示您詢問,直到獲得有效答案為止。
def yes_or_no():
reply = ''
while reply not in ['y', 'n']:
reply = input(question+' (y/n): ').lower().strip()
if reply == 'y':
return True
if reply == 'n':
return False
如果要在退出時退出程序,可以執行以下操作:
import sys
sys.exit() # replace the 'return False' above with this statement
然后要使其連續運行直到您退出,只需將要重復的代碼放在while循環中即可。
解決Johnny Mopp指出的問題后,您可以執行
def yes_or_no(reply):
reply= 'y'
while "the answer is invalid":
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
while True:
Welcome= 'Hello and welcome to one rep max calculator, Please follow the prompt below'
print (Welcome)
reps= int(input('Rep Count?'))
initial_weight= int(input('Weight lifted?'))
bench_max=(.0333 * reps + 1) * initial_weight
print (bench_max)
reply= 'y'
question= 'Do you want another calculation?'
if (not yes_or_no(reply)):
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.