0

I wrote the following program in Python but I got this error please guide me

age= (input("please inter your age:" ))
if age > 40:
    print ("old")
elif 40>age>=30:
    print ("middle-age")    
elif 30>age>=20:
    print ("yong")
TypeError: '>' not supported between instances of 'str' and 'int'

I checked the program several times

001
  • 13,291
  • 5
  • 35
  • 66
  • Why Java tag, since code is in Python? – Pradeep Simha Dec 07 '22 at 14:22
  • 2
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – 001 Dec 07 '22 at 14:26
  • Does this answer your question? [Python - 'TypeError: '<=' not supported between instances of 'str' and 'int''](https://stackoverflow.com/questions/51249024/python-typeerror-not-supported-between-instances-of-str-and-int) – Daraan Dec 11 '22 at 19:01

2 Answers2

1

the age variable is of string data type. You need to convert it to int before comparing it using operators like this -

age= int(input("please inter your age:" ))
if age > 40:
    print ("old")
elif 40>age>=30:
    print ("middle-age")    
elif 30>age>=20:
    print ("yong")

The int() before the input statement will convert the data entered by the user from string to int type.

rushilg13
  • 11
  • 2
0

while doing comparison change the age type to int, Since you input age with use of input which returns string.

age= (input("please inter your age:" ))
if int(age) > 40:
    print ("old")
elif 40>int(age)>=30:
    print ("middle-age")    
elif 30>int(age)>=20:
    print ("yong")

This gives you the expected output you want

output:

please inter your age:45
old
y051
  • 184
  • 5