-1

I am trying to get this bit of code to work to validate the following input. I want to only accept inputs 1,2,or 3. Here is what I have so far:

number = int(input('Enter a number:'))
done = False
while not done:
    try:
        if number < 3:
            done = True
    except:
        number = input("Please enter a valid number:")

The expected out put that I want if the input to loop until I get either 1,2, or 3. Right now it won't do anything to when I input something greater than three. I want to use this number as an input to another function. Any help would be great of if you need more information please let me know!

flakes
  • 21,558
  • 8
  • 41
  • 88
  • I'm guessing your issue is not converting the second call to `input` in the `except` block to an `int` – flakes Oct 01 '22 at 05:14

1 Answers1

0

Try this

number = int(input('Enter a number:'))
done = False
while not done:
    if number <= 3:
        done = True
    else:
        number = int(input("Please enter a valid number:"))

except block only run when there is an error in try block code.

Use if-else statement.

You can use this one too.

number = int(input('Enter a number:'))
done = False
while number>3:
    number = int(input('Please enter a valid number:'))

codester_09
  • 5,622
  • 2
  • 5
  • 27