2017-03-27 3 views
0

Ich versuche, Python 3 zu lernen, und ich frage mich, wie ich würde Schleife etwas ...Python 3, die Hilfe benötigt, etwas Looping

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.') 

ich es machen will, so dass, wenn es sagt: " Du hast keine Nummer eingegeben. " es springt zurück zu "Wie viele Katzen hast du?"

+0

haben Sie nachgeschlagen ‚Python Loops‘? – AsheKetchum

+1

Warum willst du nichts drucken, wenn es 3 Katzen gibt? –

Antwort

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') 

Das hält für immer Looping (Wegen der while True) und geht nur dann, wenn eine gültige Nummer eingegeben wird, wir die Schleife entkommen mit break