如何让我的程序复位,并添加一个继续提示,将自动重新启动程序

2024-05-23 17:03:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我创造了一个一次最大的计算器为举重。一切运行正常,但我希望它以一个重启提示结束,以防用户想要计算另一个电梯。(请记住,我对编码和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)

没有错误消息,但是程序不会重新启动,不管我是否点击yes


Tags: orthenoinputreplymaxinitialyes
2条回答

一旦你解决了约翰尼·莫普指出的问题,你就可以

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

这将使提示询问,直到你得到一个有效的答案。你知道吗

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循环中。你知道吗

相关问题 更多 >