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 choicenot being"y", and condition_2 is the choicenot being"n",
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
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.
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…
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.
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.
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