Search code examples
pythoncomparison-operators

Comparison Operator in Python


I am learning python. I want to write a code where if my user is between the age of 18 and 22, they become eligible. If they are below 18, the underage message will display and similarly if they are more than 22 the overage message will display. This is the code I have written enter image description here

While running the code, if I put my age as 10 I get the correct response ie, 'sorry You are underage' but the same does not happen when I input 9 as my age. What am I missing here?


Solution

  • user_age = int(input("please enter your age"))
    

    input funtion return string so that we are converting into int.

    you are checking string for logical operator. it won't work

    try this.

    if user_age > 0 and user_age < 18:
        print("under age")
    elif user_age >= 18 and user_age <= 22:
        print("congrats")
    else:
       print("over age")