How can I check for two conditions at once?

Question

How can I check for two conditions at once?

Answer

If we want two things to be True before entering a loop, we can use the and operator. Recall that and requires both sides to be True for the entire condition to be True, otherwise it is False.
We’d write condition_1 and condition_2, where condition_1 is the choice not being "y", and condition_2 is the choice not being "n",

3 Likes

But why is there an “and” operator needed in that exercise?
https://www.codecademy.com/courses/learn-python/lessons/loops/exercises/simple-errors?action=resume_content_item
Wouldn’t a logical “and” not insist that both answers are true? Like ‘y’ and ‘n’ would mean that there have to be a ‘y’ AND an ‘n’. I am a bit confused :confused:

8 Likes

yes, but you check if the user input does not equal y and n

if the user enters something invalid (like x), both condition are true, True and True is true, so the loop will run again. Which should happen, given we need to get new input

if the user enters valid input (for example y) one of the conditions is True while the other is False. True and False is false, so the loop stops (which it should, we have valid input)

on the other hand, using or will always result in true (True or True, True or False both evaluate to true), so the loop will keep running forever

30 Likes

Because if you press “y”, you are not pressing “n”… so it will result to an error

Hey guys, thank you very much for your rapid answers.
Now I noticed the failure in my thinking.
Best Regards

2 Likes

Why cant the “or” be used here?

like choice != ‘y’ or ‘n’

1 Like

that is even worse, 'n' isn’t compared to anything, so python will simple see if 'n' evaluate to true, which it does:

if 'n':
   print True

strings are evaluated as true.

2 Likes

okay this makes sense why I messed up now. its not doing the “choice !=” on the other side of the “or”. It needs something to check on that side as well.

1 Like

yes, and you should use and not or. But i already explained that

1 Like

Thank you for knocking some sense into me. Really, I appreciate it. I never really understood booleans well and I’ve learned about boolean logic more during these couple of days than I’ve learned in years. I always thought it works differently which is probably why my programs often don’t work properly… :expressionless:

on one hand, you are most welcome :slight_smile:

on the other hand, Boolean and comparison are some of the very fundamentals of any programming language. If you have been programming for multiple years, you should certainly know them.

2 Likes

I know, but I’ve been learning by trial and error most of that time and this particular part was always my weakest point. Maybe I was looking at wrong places, but I didn’t find any good explanation that would go deep into how this actually works and I always misunderstood it and this is the first time that I found a good explanation that made sense to me and helped me to understand it better, so hopefully I will avoid making the same mistakes in the future.

That helps so much. Thanks @stetim94

choice = raw_input('Enjoying the course? (y/n)')

chicken = ['y','n']

if chicken:

  chicken == True

else:

  chicken == False

while chicken: 

  choice = raw_input("Sorry, I didn't catch that. Enter again: ")

Why does this not work?

Why would this work? So we get input from the user (choice, which is a string)

then we check if the list is not empty:

if chicken:

which it isn’t, so this condition/if clause is always true

then we check if a list and a boolean are equal:

chicken == True

which isn’t the case, so we get False, but nothing with this result.

then again, the while loop:

while chicken: 

is always true, given the list is not empty.

Why are there exclamation marks???

Because the exclamation marks are the not operator, so === will check if equal, while !== will check if the values are not equal

I’m having the same problem with this lesson that I was having with the Battleship project previously. After several failed attempts to run due to “program taking too long to terminate”, I check the solution and copy the solution, (which is the same as what I had), it then checks this off and lets me move on, but when I try to run THEIR solution, it still says program took too long to terminate. Is this a bug that will last the rest of the Python lessons and projects? It’s highly frustrating and I don’t know where the issue is!

sometimes there is a problem with raw_input or input, to overcome this problem you can sometimes overcome this problem by hard-coding the value instead of relying on input

Ah OK I see, thank you very much for you reply!!