Looking for answer to Question 4 of conditionals and control flow quiz

this brought back ‘nothing’
Could someone explain this further please? The answer said that the if and the two elifs were False and would print ‘nothing’.

if not True:
print True
elif 8 % 3 < 2:
print False
elif False and True:
print “None”
else:
print “Nothing”

Thanks in advance

Hi welcome to the forum!

For future reference, remember to press </> before pasting, since it’s easier for people to read and help with.

if not True:
    print True
elif 8 % 3 < 2:
    print False
elif False and True:
    print “None”
else:
    print “Nothing”
  • not True evaluates to False. Therefore this code block is not run.

  • 8 % 3 = 2, so 8 % 3 < 2 evaluates to False. Therefore this code block is not run.

  • False and True evaluates to False. Why? If we abstract the condition into x and y, in order for x and y to return True is if both are true. So since False is False, one of the conditions is not True, and therefore an overall False is returned. Therefore this code block is not run.

  • else: print “Nothing” – Because all other conditions are False, the else code is executed, "Nothing" is printed.

2 Likes

Hi there,

thank you for your prompt reply. Even though they all brought back false why did it return ‘nothing’ After completing few of these exercises, the if always had something above it (variable, raw_input etc) but the question just showed this block of code?
I’m just a little confused how this block of code differs as it doesn’t explain it with the course (I think)

thanks again

Hi, let me just make sure I’m understanding your question correctly:

Even though they all brought back false why did it return ‘nothing’

The else block in an if-else clause is only executed when the if condition is False. Because all the if conditions in the code were False, the else code was run, which was to print “Nothing”. In a more simplified example:

if a:
    print a
elif b:
    print b
elif c:
    print c
else:
    print d

In the above example, if a, b, and c are false, the result is to print d.

If one thinks about it in a pure English sentence, it can be read thus:
If a, b, or c are True, do (a,b,c respectively) for me. If it’s anything else, do (d).

1 Like

Thanks, its a lot clearer now

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