我正在尝试学习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 :(得分: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