使用一段时间后代码无法正常工作:try:循环



我不得不为学校做一个蟒蛇练习。

这是我的代码:

print("Animal-determiner")
while True:
try:
amount =  int(input("How many animals do you want to determine? "))
except ValueError:
print("That was not a number.")
else:
break
while amount > 0:
legs = int(input("How may legs does the animal have? "))
if legs < 0:
print("Such an animal doesn't exist.")
elif legs == 1 or 3:
print("Such an animal doesn't exist.")
elif legs == 0:
water = input("Does the animal live in water? ")
if water is "yes":
print("This is a fish")
elif water is "no":
print("This is a snake.")
else:
print("You didn't put in yes or no.")
elif legs == 2:
fly = input("Can the animal fly? ")
if vliegen is "yes":
print("This is a bird.")
elif vliegen is "no":
print("This has to be a bird, which can't fly.")
else:
print("You didn't put in yes or no.")
elif legs == 4:
print("This is getting to complicated, I'm stopping")
amount -= 1

但是当我运行这段代码时,它总是说:"这样的动物不存在。 即使我输入 a:0 或 4 应该会回馈一些不同的东西

帮助将不胜感激。

条件elif legs == 1 or 3:是错误的,应该是elif legs == 1 or legs == 3:elif legs in (1, 3):

legs == 1 or 3- 那不是那样工作的。3将作为条件进行评估。因为它不为零,所以它的计算结果为Trueor并不像您想象的那样工作 - 它用于检查是否True两个条件中的至少一个。并且因为3True,所以表达式会True。使用in

legs in (1, 3)

in检查某些内容是否为集合。

最新更新