小编Lia*_*ton的帖子

Python - 如果输入的值不正确,则重新启动if语句

所以我目前正在学习如何使用Python,并一直在尝试解决我的问题,我有一个if语句,当输入一个不正确的值时,我希望它重新启动并再次提出问题.

我相信这需要一个while循环或for循环,但是在寻找一段时间之后,我只是不确定如何使用此代码实现它,因此如果有人知道我很想知道如何.

x = int(input("Pick between 1,2,3,4,5: "))

if x == 1:
    print("You picked 1")
elif x == 2:
    print("You picked 2")
elif x == 3:
    print("You picked 3")
elif x == 4:
    print("You picked 4")
elif x == 5:
    print("You picked 5")
else:
    print("This is not a valid input, please try again")
    #Want to go back to asking the start question again
Run Code Online (Sandbox Code Playgroud)

谢谢,

利亚姆

python loops if-statement

3
推荐指数
1
解决办法
1031
查看次数

标签 统计

if-statement ×1

loops ×1

python ×1