0

So this is my code:

print('How many cats do you have?')
numCats = input()
try:
    if int(numCats)>=4:
        print('That is a lot of cats.')
    elif int(numCats)<0:
        print('You can\'t have negative cats, dumbo')
    else:
        print('That is not that many cats')
except ValueError:
    print('You did not enter a number')

Currently, when I enter 'six', it outputs just the error message 'You did not enter a number' and the code ends there. I want the code to loop back to asking 'how many cats do I have' until I enter a non-error answer. Not sure how to do this. Would be great if anyone could offer this newbie some advice.

Mainly, I would like to know how to except a Negative Error, instead of writing an elif statement for it.

Lse Library
  • 13
  • 1
  • 5
  • There is no "Negative Error", unless you throw one (which would also require a conditional) – jonrsharpe Apr 08 '18 at 15:39
  • What about this? while True: print('How many cats do you have?') numCats = input() try: if int(numCats)>=4: print('That is a lot of cats.') break elif int(numCats)<0: print('You can\'t have negative cats, dumbo') else: print('That is not that many cats') break except ValueError: print('You did not enter a number') – Lse Library Apr 08 '18 at 15:41
  • You can use the Tester-Doer Pattern, this basically means that you call a Function, which returns true if it was successfull and false otherwise. You can then call the method until it returns true – Maaaaa Apr 08 '18 at 15:42
  • What about it? If you want to know how to manually throw exceptions, [read the docs](https://docs.python.org/3/tutorial/errors.html). – jonrsharpe Apr 08 '18 at 15:42

0 Answers0