user3352353
user3352353

Reputation: 265

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?

Upvotes: 0

Views: 434

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124748

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

Upvotes: 6

Related Questions