Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading