正如您所知,我是一个相当新手的Python编码器。截至目前,我正在制作一些小测试程序,并尝试在尝试任何大型程序之前确保所有缩进和正确完成的任何操作。 所以我试图确保如果输入不是整数,那么程序会输出一条消息,说明必须输入一个整数。但是,对于我当前的代码(我认为应该是正确的),无论答案如何,都输出“请输入整数”消息。我做错了什么?这是代码:
a = input("What is your age?")
b = 7
c = ((a-2) * 4) +25
if a == int:
print "Your age in a small dog's years is:", ((a-2) * 4)+28
print "Your age in a medium sized dog's years is:", ((a-2) * 6)+27
print "Your age in a big dog's years is:", ((a-2) * 9)+22
print "Your age in cat years is:", c
print "Your age in guinea pig years is:", a * 15
print "Your age in hamster years is:", a * 20
print "Your age in pig years is:", ((a-1) * 4)+18
print "Your age in goldfish years is:", ((a-1) * 8)+188
print "Your age in cow years is:", ((a-1) * 4)+14
print "Your age in horse years is:", a * 3
print "Your age in goat years is:", ((a-1) * 6)+18
print "Your age in rabbit years is:", ((a-1) * 8)+20
print "Your age in chinchilla years is:", ((a-1) * 7)+17
elif a != int:
print "You must enter an integer!"
除此之外,它只是这个小小的两行似乎毁了它。 感谢。
答案 0 :(得分:3)
Python 3:
a = input("What is your age?") # this is a string input
a = int(input("What is your age?")) # you need an integer input
elif(isinstance(a, int)) # this is how you check if a is integer
对于Python 2中的情况:
a = raw_input("What is your age?") # input is interpreted as unicode here
try:
a = int(a)
b = 7
c = ((a-2) * 4) +25
print "Your age in a small dog's years is:", ((a-2) * 4)+28
except:
print "You must enter an integer!"