Home > Software design >  Error input output while excuting Python"
Error input output while excuting Python"

Time:12-17

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related