Python for Everybody - Conditional Execution

Tell us what’s happening:

Hello everyone. Kindly walk me through on this.

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15

Challenge Information:

Python for Everybody - Conditional Execution

if 0 == x:
if y == 10:
print("Yes")

if 0 == x:
    if y == 10:
    print("Yes")

if 0 == x:
if y == 10:
    print("Yes")

if 0 == x:
    if y == 10:
        print("Yes")

Ok, let’s apply simple indentation logic.
When you have an if statement (for example), it will execute everything which immediately follows which is indented by four spaces, subject to the condition in the statement.

Going through the statements in order:

  1. Nothing is indented, so both conditional statements are meaningless. There is no code which is conditional on either if statement. The print statement will be executed regardless.
  2. Both indented lines will be executed if the first if statement is true. The second if statement is meaningless as there is no code which is conditional on it.
  3. The first if statement is meaningless as there is no indented code which directly follows it. The print statement will execute if the second if statement is true.
  4. If the first if statement is true, the second will be executed. If the second is true also, the print statement will be executed.

Does that make sense?

1 Like

Thank you so much. makes lotta sense. I’m somewhat a new pythonista, but I’ll crack along.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.