繁体   English   中英

如何让我的程序重置并添加继续提示,该提示将自动重启

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM