创建了一个单倍最大功率提升计算器。一切运行正常,但我希望它以重新启动提示结束,以防用户想要计算另一个升程。 (请记住,我是编码的新手,只学习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)
没有错误消息,但是无论我按是还是否,程序都不会重新启动。
答案 0 :(得分:0)
这将提示您询问,直到获得有效答案为止。
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循环中即可。
答案 1 :(得分:0)
一旦您解决了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