Please explain the how I should think here

I just took the quiz and got all right except one question where I cannot understand the way I’m supposed to think, which probably means that i have not completely understood how the control flow thing works…

Can somebody please explain to me as simple as possible the answer to that question (see attached picture) and also why the explanation in the attached picture is like it is?
Best regards and thanks in advance

1 Like

You’re being asked to evaluate the branches of the if...else structure, so let’s break it down.

if not True:
    print True

The code inside the if statement only runs if the condition is True. not True = False, so we don’t run this block.

elif 8 % 3 < 2:
    print False

8 % 3 is asking for the remainder when you divide 8 by 3, which is 2. It’s then checking whether 2 is less than 2, which it isn’t so we don’t run this block either.

elif False and True:
    print "None"

Again, we need the condition to be True to run this block. and will only return True if both conditions either side are True. False and True evaluates to False, so we don’t run this block.

Therefore, we arrive here:

    print "Nothing"

and we see “Nothing” gets printed to the console.

Does that help? :slight_smile:

Ok, so is this the general case on how it works? I mean, if I understood it correctly, in an if…else structure, it will only “run the command” (in this case the command is print) ONLY if the if statement = True?
So basically, the if…else structures depend on what a boolean variable gives (True or False) and will not run if the result is False?

Yes, the code contained inside an if or an elif block will only run if the condition is met - i.e. it is True once fully resolved.

As a real-world analogy, let’s say you’re doing a lunch run to Subway* and your colleague Bob wants you to pick him up a sandwich.

Bob says: “I’d like a foot-long Subway Melt on Hearty Italian bread, or regular Italian if they’re out of that. 9-grain wheat is OK, but it’s not my favourite. Lettuce, onion, tomato and peppers as well, oh and honey mustard sauce please. Mayo is fine if they don’t have that sauce.”

Bob’s order can be expressed using if-else statements:

sandwich = "Subway Club"
bread = ""
sauce = ""

if they_have_hearty_italian_bread == True:
     bread = "Hearty Italian"
elif they_have_italian_bread == True:
    bread = "Italian"
    bread = "9-Grain Wheat"

if they_have_honey_mustard_sauce == True:
    sauce = "Honey Mustard"
elif they_have_mayo == True:
    sauce = "Mayo"
    sauce = None

Let’s not forget about the not operator. For example:

am_i_hungry = False

while not am_i_hungry:

not am_i_hungry here resolves to not False, which is True so the while loop runs. If we become hungry, and flip the value of am_i_hungry to True, the while loop condition is no longer met and we run the else block of buy_lunch().

Does that help at all? Hopefully it does. Now, if you’ll excuse me, I need to go buy a sandwich…

(* other food vendors are available.)