-1

So I have the following function in my script:

def age():
    global age
    age = raw_input("Age: ")

    if age == 14:
        sleep(1)
        print ("LOL, same")
    
    elif age < 18:
        sleep(1)
        print ("This test is made for contestants older than ten")
        introduction()  
        
    elif age > 18:
        sleep(1)
        print ("Geez grandpa, you sure are old")

When I run this, it registers every age I type as above 18 like so:

Could you tell me your age?

Age: 4

Geez grandpa, you sure are old

Why does it do this?

Community
  • 1
  • 1
TomD
  • 11
  • 2
  • 1
    Change `age = raw_input("Age: ")` to `age = int(raw_input("Age: "))`. The result of `raw_input` is a string, which Python 2.7 will allow you to compare with an `int` – roganjosh Feb 05 '17 at 10:22
  • `raw_input()` returns a string, not an integer. You'll have to convert it first if you want to compare that with other integers. See the duplicate. – Martijn Pieters Feb 05 '17 at 10:23
  • 1
    You should use Python 3; it won’t fail silently on these types of errors. – Ry- Feb 05 '17 at 10:23

2 Answers2

3

As raw_input returns the user input as a string type, you should cast it into int.

Change this :

age = raw_input("Age: ")

To:

age = int(raw_input("Age: "))
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
0

your raw_input is taken as string

age = raw_input("Age: ")

so you need to convert it into a integer value if you want to compare it against numbers..

age = int(raw_input("Age: "))
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97