0

Hi I'm very new to coding in python, but I can't find out why this code doesn't work.

prompt = "> "

def pos(answer):
    print answer
    if 0 < answer < 12:
        print "Damn you'r young, but you can still do a lot of things.\n"
    elif 65 < answer < 110:
        print "Wow you'r old, but never to old to learn something.\n"
    elif answer < 0:
        print "You should input a positive number."
    else:
        print "I didn't understand you, try inputting a number."


print "Hi there, what's your name?"
name_a = raw_input(prompt)

print "Hi %s, how old are you?" % name_a
age = raw_input(prompt)
pos(age)

For every input i give it will go to else and print "i didn't understand you...".

2 Answers2

0

Since you're using python 2 (there's no raw_input in python 3), raw_input returns a string. To get an integer from input, use input.

prompt = "> "

def pos(answer):
    print answer
    if 0 < answer < 12:
        print "Damn you'r young, but you can still do a lot of things.\n"
    elif 65 < answer < 110:
        print "Wow you'r old, but never to old to learn something.\n"
    elif answer < 0:
        print "You should input a positive number."
    else:
        print "I didn't understand you, try inputting a number."


print "Hi there, what's your name?"
name_a = input(prompt)

print "Hi %s, how old are you?" % name_a
age = input(prompt)
pos(age)

Side note: Use int(input()) for python 3

If for some reason you want the code to run in both python 2 and python 3, try this

raw_input = input # since `input` exist in both python 2 and python 3
age = int(raw_input("..."))
Gareth Ma
  • 707
  • 4
  • 10
-1

do

prompt = "> "

def pos(age):
    answer = int(age)
    print answer
    if 0 < answer < 12:
        print "Damn you'r young, but you can still do a lot of things.\n"
    elif 65 < answer < 110:
        print "Wow you'r old, but never to old to learn something.\n"
    elif answer < 0:
        print "You should input a positive number."
    else:
        print "I didn't understand you, try inputting a number."


print "Hi there, what's your name?"
name_a = raw_input(prompt)

print "Hi %s, how old are you?" % name_a
age = raw_input(prompt)
pos(age)
Sven
  • 1,014
  • 1
  • 11
  • 27