Input Statements

Lets say I have the input statement:

num = input(Press 1 or 2)

How do I create an error message that repeats so that if they pick an option thats not given such as 3

If num != 1 and num != 3:
    num = input(invalid key. Try again)

except when the error message comes up it accepts invalid inputs

I just used a while statement

Recall that it takes a str value as an argument.

input("string value")

Using a while loop with a couple of prewritten prompts helps keep the inputs contained.

prompt = ['Enter 1 or 2: ', 'Invalid input... Enter 1 or 2: ']
i = 0
num = '0'    # note that an empty string won't work
while num not in '12':
    num = input(prompt[i])
    i = 1
num = int(num)
1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.