对于代码中的“狗”部分,它完美地工作并完成它应有的功能。但是,如果在开始时为输入问题输入“Cat”,它仍会继续并执行代码的狗部分。使用if/elif语句遇到问题
即使我在代码中写道,如果问题==“猫”或“猫”的答案,那么它应该做这个部分而不是狗部分。
import time
import sys
animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")
if animal=="Dog"or"dog":
age=int(input("How old is your Dog?"))
if age==1:
print("Calculating the age of the Dog...")
time.sleep(1)
print("The age of the animal is: 11")
elif age==2:
print("Calculating the age of the Dog...")
time.sleep(1)
print("The age of the animal is: 11")
else:
age=age-2
print("Calculating the age of the Dog...")
time.sleep(1)
agecalculation=age*4+22
print("The age of the animal is:",agecalculation)
time.sleep(2)
print("End of program.")
time.sleep(2)
sys.exit()
elif animal=="Cat"or"cat":
age=int(input("How old is your Cat?"))
if age==1:
print("Calculating the age of the Cat...")
time.sleep(1)
print("The age of the animal is: 15")
elif age==2:
print("Calculating the age of the Cat...")
time.sleep(1)
print("The age of the animal is: 25")
else:
age=age-2
print("Calculating the age of the Cat...")
time.sleep(1)
agecalculation=age*4+25
print("The age of the animal is:",agecalculation)
time.sleep(2)
print("End of program.")
time.sleep(2)
sys.exit()
else:
print("That is not an animal, or isn't on the list specified")
animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")
请使用一致的缩进(每个级别总是4个空格)。这在Python中具有语义相关性。 – Nabla
'如果动物==“狗”或动物==“狗”:'但也许你应该写'如果animal.lower()==“狗”:' – pasztorpisti
感谢您的快速回复pasztorpisti - 第一部分解决了它完美。荣誉给你。 – user3376281