Following along with Al Sweigarts python lessons and tried modifying his cat code some. I can input the "except ValueError" fine using just if and elif statements but I think using a while statement I am messing something up. I want this simple code to repeat when the user enters something incorrectly which is working so far. I just need to put in something that address a non integer as the input.
Is it something to do with break/continue statements not being used?
print('How many cats do you got')
numCats = int(input())
while numCats < 0:
print('That is not a valid number')
print('How many cats do you got')
numCats = int(input())
if numCats >= 4:
print('That is a lot of cats')
elif numCats < 4:
print('That is not a lot of cats')
except ValueError:
print('That was not a valid number')
I would just like the code to repeat if entering an invalid number while also repeating after a non-integer value. I can't get past the except ValueError part though. Thanks!
An except
block requires a try
block. It's within the try
block that you find an exception and if found the except
clause is run.
while True:
try:
print('How many cats do you got: ')
numCats = int(input())
if numCats >= 0:
break
else:
print('That was not a valid number')
except ValueError:
print('That was not a valid number')
if numCats >= 4:
print('That is a lot of cats')
elif numCats < 4:
print('That is not a lot of cats')