如何在触发else语句时重新启动此程序?

2024-04-20 03:37:11 发布

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

我很好奇如何让这个程序在触发else语句后自动提示用户输入另一个值。你知道吗

tires = float(input('How many tires would you like?'))
if tires == 2:
    print('That will be $250')

if tires == 4:
    print('That will be $400')

else:
    print('That is not a valid input, please try again')

Tags: 用户程序inputifthat语句befloat
1条回答
网友
1楼 · 发布于 2024-04-20 03:37:11

将整个内容包装成while循环:

while True:
    tires = float(input('How many tires would you like?'))

    if tires == 2:
        print('That will be $250')
        break

    elif tires == 4:
        print('That will be $400')
        break

    else:
        print('That is not a valid input, please try again')
        # keep looping

相关问题 更多 >