1

I'm trying to make a simple program where I'm asking to user to put name and age and after that using if else to print the result.

I want when is put age 20 so result should come

Congratulation Edit your age is 20 and you can find the truth

but It is not working here is the code which I'm using.

Code

name = input("what is your name \n")
print('Nice to meet you', name, "!!")
age = input("Your Age ? \n")

if age > 18:
    print('Congratulation ' + name + ' your age is '+ str(age), ' and you can find the truth ')
else:
    print('Sorry ' + name + 'your age is '+ str(age), ' and you can also find the truth if you start from today')

and error which I'm getting after running my code.

Error

what is your name 
Edit
Nice to meet you Edit !!
Your Age ? 
20
Traceback (most recent call last):
  File "C:/Users/hp/PycharmProjects/Project/input_user.py", line 5, in <module>
    if age > 18:
TypeError: unorderable types: str() > int()

Help me out here to resolve this problem.

Neha Manchal
  • 39
  • 1
  • 5

1 Answers1

5

Read the error message:

TypeError: unorderable types: str() > int()

You are trying to compare a string and an integer. You have to convert the input to int first:

age = int(input("Your Age ? \n"))

This will allow you to compare it with numbers.

Alternatively, convert it when you need to compare with numbers

if int(age) > 18:

But this is really more of a usability issue now. It would be better to simply convert it to int as soon as you get it, as age you probably need the numerical value more than the string value.

Arc676
  • 4,445
  • 3
  • 28
  • 44