0

I'm trying to learn Python 3 and I'm wondering how I would loop something...

print("how many cats do you have?")
numCats = input()
try:
    if int(numCats) >= 4:
        print('That is a lot of cats.')
    elif int(numCats) in range(1, 3):
        print('That is not that many cats.')
    elif int(numCats) == 0:
        print("Oh, you have no cats.")
except ValueError:
    print('You did not enter a number.')

I want to make it so that when it says, "You did not enter a number." it loops back to "How many cats do you have?"

B. Boy
  • 9
  • 2

1 Answers1

1
while True:
    print("how many cats do you have?")
    numCats = input()
    try:
        if int(numCats) >= 4:
            print('That is a lot of cats.')
        elif int(numCats) in range(1, 3):
            print('That is not that many cats.')
        elif int(numCats) == 0:
            print("Oh, you have no cats.")
        break
    except ValueError:
        print('You did not enter a number.\n')

This keeps looping forever (Because of the while True) and proceeds only when a valid number is entered, we escape the loop using break

Elia Perantoni
  • 581
  • 1
  • 6
  • 19