如果输入错误,我怎样才能再次询问相同的输入?

时间:2014-03-02 02:09:32

标签: python python-3.x

players = input("How many players?")
if players == "1":
    p1 +=1
elif players == "2":
    p2 +=1
else:
    print("Invalid Input")
    players = input("How many players?")

如果输入无效输入,我怎样才能重复其他?

1 个答案:

答案 0 :(得分:6)

输入时使用无限循环和break

while True:
    players = input("How many players?")
    if players == "1":
        p1 += 1
        break
    elif players == "2":
        p2 += 1
        break
    else:
        print("Sorry, please pick 1 or 2. Let's try again")