-1

As you can tell, I am a fairly novice Python coder. As of current I am just making some little test programs and trying to ensure all my indentation and whatever is done properly before trying any large programs. So I am trying to ensure that if an input is not an integer, then the program outputs a message saying an integer must be inputted. However, with the current code I have (which I think should be correct) regardless of the answer the "please input an integer" message is outputted. What have I done wrong? Here is the code:

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!"

It works otherwise, it's just this little end two lines that seem to ruin it. Thanks.

Van Peer
  • 2,057
  • 2
  • 22
  • 34
  • 1
    `a == int` is not the way you check the type of a variable. – Barmar Nov 23 '17 at 19:17
  • https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not – Pavel Nov 23 '17 at 19:17
  • make sure your input are integers `int(input())`. With your current implementation check `type(a) and you will see it says string` – mugabits Nov 23 '17 at 19:18

1 Answers1

0

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

For your case in 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!"
Snow
  • 938
  • 15
  • 41