我正在尝试编写一个程序,可以将狗的年龄转换为人类的年龄,并且当我尝试测试它时。它没有给我任何提示。我不知道下一步该做什么以及我的代码哪里出了问题:
def calculator():
dog_age = input("Please enter dog age ")
try:
dog_age = float(input("what is your dog age?: "))
if dog_age < 0:
print("age can not be a negative number")
elif dog_age <= 1:
print(dog_age * 15)
elif dog_age <= 2:
print(dog_age * 12)
elif dog_age <= 3:
print(dog_age * 9.3)
elif dog_age <= 4:
print(dog_age * 8)
elif dog_age <= 5:
print(dog_age * 7.2)
elif dog_age >= 5:
print(dog_age * 7)
print("The given dog age", dog_age, "is", round(dog_age, 2),"in human years")
except ValueError:
print("please enter a valid number")
您需要执行计算器功能:
def calculator():
dog_age = input("Please enter dog age ")
try:
dog_age = float(input("what is your dog age?: "))
if dog_age < 0:
print("age can not be a negative number")
elif dog_age <= 1:
print(dog_age * 15)
elif dog_age <= 2:
print(dog_age * 12)
elif dog_age <= 3:
print(dog_age * 9.3)
elif dog_age <= 4:
print(dog_age * 8)
elif dog_age <= 5:
print(dog_age * 7.2)
elif dog_age >= 5:
print(dog_age * 7)
print("The given dog age", dog_age, "is", round(dog_age, 2),"in human years")
except ValueError:
print("please enter a valid number")
if __name__ == "__main__": # NEW
calculator()