2017-03-27 58 views
0

我想學習Python 3,我不知道我怎麼會循環的東西...Python 3中,需要幫助的東西循環

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

我想,這樣當它說,使其「你沒有輸入號碼。「它會回到「你有多少貓?」

+0

你擡頭「蟒蛇循環」的循環進行? – AsheKetchum

+1

如果有3只貓,爲什麼不打印任何東西? –

回答

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

這使無休止的循環下去(因爲while True),並輸入一個有效的數字,只有當我們逃避使用break