-3

I am new to Python programming. I am writing the code below, and when I execute it the IDE returns an error message : TypeError: unorderable types: str() < int()

Code below :

print("What is your name?")


name = input()

print("What is your age?")

age = input()

if name=='Jack':

  print ("Hello Jack")

elif age<12:

    print("You are not Jack")

The error

    elif age<12:
TypeError: unorderable types: str() < int()
3
  • 3
    It seems that you are comparing a string with an integer. Nov 1, 2016 at 21:33
  • 1
    You need to cast your age input to an int.
    – idjaw
    Nov 1, 2016 at 21:35
  • 1
    So many duplicates of this, but where to find them?
    – user707650
    Nov 1, 2016 at 21:35

2 Answers 2

1

Tip:

print('something')
input()
# same as
input('something')

Then, input returns in python 3 a string. And you cannot compare a string with an int.

It's like if you were doing '5' < 2. You need to transform '5' into an int. And it's pretty easy: int('5') == 5

name = input("What is your name?")

age = input("What is your age?")

if name == 'Jack':
     print("Hello Jack")

elif int(age) < 12:

    print("You are not Jack")

Matt

0

input() returns a string. You cannot directly compare a string to an integer.

Convert age to an integer by calling int():

age = int(input())

Not the answer you're looking for? Browse other questions tagged or ask your own question.