-3

In my program I need a hand in trying to get python to say that a input is invalid, the code asks for numbers, if you input a letter I would like it to say its invalid rather than breaking, is there any way I can do this?

Kylo ren
  • 1
  • 3

1 Answers1

0

Here is some code that works using a try/except block. The code attempts to convert the input into a number, if it succeeds it breaks out of the loop, if not the error is caught and an error message is printed, and then the loop starts again

passed = False
print "input a number"
while not passed:
    a = raw_input()
    try:
        number = int(a)
        passed = True
        print "Number inputted"
    except(ValueError):
        print "You did not input a number, try again"
user3088440
  • 144
  • 7