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

Why does the if statement run even if the input does not match the elements in the list?

I am trying to get the input from the user and then checking if the input is present in the two lists. For some reason even if the input is ‘x’ the output is ‘yes. I am not sure why this is happening. What am I doing wrong?

texas  = ['austin', 'houston', 'dallas']
Newyork = ['albany','stony', 'nyc']
city  = input('enter city name?\n>>')
if city in Newyork or texas:
    print('yes')
else: 
    print('no')

>Solution :

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

Try this:

texas = ['austin', 'houston', 'dallas']
Newyork = ['albany','stony', 'nyc']
city = input('enter city name?\n>>')
if city in Newyork or city in texas:
    print('yes')
else: 
    print('no')

Explanation: you have to check twice: if city in Newyork and if city in texas

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