简体   繁体   中英

How can I ask for the same input again when there was incorrect input?

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

How can I get the else to repeat when an invalid input is entered?

Use an infinite loop and break out when you have your input:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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