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

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.