Exercise 35 if-elif-else & Boolean logic

when I input “taunt bear” in the Terminal, it directly goes with the first elif instead of the second one since they both are “taunt bear” is this because of the definition that ( Python searches for the first true condition and carries out its associated block of statements)

  1. the boolean part

    choice == “taunt bear” and not bear_moved

    does this contain two booleans to evaluate:
    1. “and”: taunt bear and not bear_moved

             2. "=="

    when I input from the terminal it runs with input “taunt bear” even without “not bear_moved” isn’t it suppose to evaluate both since it is “AND” both have to be true so that it gets true as the answer.

It’s been the whole afternoon that I am stuck at this part. I tried to change the “and” to “or” it gives me the else print(“I got no idea what that means ) as the answer. I also change the second “taunt bear” to " laugh at bear” as something different from the first one to see if it will work but still give me “I got no idea what that means” as answer

1 Like

To understand better, try Changing that initial “bear_moved” to None.

Not is like an on off switch. It’s checking if something is ‘on’ or if it is ‘true’.

Now try changing bear_moved to:

The not will see that something is there.

Now try bear_moved = 0.

It’s tricky. Play with it.
Don’t be afraid to alter variables to see what happens.

Yes, the and not bear_moved is part of the boolean expression, and if both of those are true then this will run. Remember the truth tables for the logic in the book? Also, it’s kind of like this:

(choice == “taunt bear”) and (not bear_moved)

I think what trips people up is they read it as:

choice == (“taunt bear” and not bear_moved)

But it’s not saying “if choice is taunt bear and choice is also not bear moved”. That sort of makes no sense right? Instead, it’s always two things around an operator, so you can think like this:

           ==                      not bear_moved
    choice    "taunt bear"

See how I have kind of a tree here? Ponder that for a bit.

1 Like

Thank you so much, Zed. I am still a little bit confused. When I input “taunt bear” in the terminal it will only evaluate choice == taunt bear part. How does it gonna evaluate “not bear_moved” part
bear_moved is assigned to False under bear_room().
First elif is choice == taunt bear and not bear_moved
Second elif is choice == taunt bear and bear_moved.
how does this part works if I input only “taunt bear”. does it goes to first elif or second elif
I think I am having a hard time understanding the logic starts from " def bear_room() until the if - else statement" ends

Ok, but you keep missing the most important word:


Focus on that, it’s the top of the tree. If I do this:

x and y

It will evaluate x, then y, then if both are true, do that else. I think you’re seeing only the x then only the y and ignoring the and.

Also, why are you not printing? Remember that you cannot see what the computer is doing by reading code. You have to print, print print, like this:

print(‘bear_moved’, bear_moved, ‘choice’, choice)

Put that at the top and watch it, and see how it changes rather than trying to decipher the code in your head. Another way to put this is, your brain is almost always wrong, so never trust it, always measure, and you measure using print.


Thank you Zed. I will remember to do more printing from now on.