Challenge - 5 Problems
If Statement Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if-else blocks
What is the output of this Python code?
Python
x = 10 if x > 5: if x > 8: print("A") else: print("B") else: print("C")
Attempts:
2 left
💡 Hint
Check the conditions step by step from top to bottom.
✗ Incorrect
x is 10, which is greater than 5, so the first if is true. Then inside, x is also greater than 8, so it prints "A".
❓ Predict Output
intermediate2:00remaining
If-elif-else execution output
What will be printed when this code runs?
Python
num = 0 if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative")
Attempts:
2 left
💡 Hint
Check which condition matches the value of num.
✗ Incorrect
num is 0, so the elif condition num == 0 is true, printing "Zero".
❓ Predict Output
advanced2:00remaining
Output with multiple if statements
What is the output of this code snippet?
Python
a = 3 if a > 2: print("X") if a > 4: print("Y") else: print("Z")
Attempts:
2 left
💡 Hint
Remember that the else belongs to the second if only.
✗ Incorrect
a is 3, so first if prints "X". Second if a > 4 is false, so else prints "Z".
❓ Predict Output
advanced2:00remaining
If statement with logical operators
What will this code print?
Python
x = 7 y = 5 if x > 5 and y < 10: print("Yes") elif x > 5 or y > 10: print("Maybe") else: print("No")
Attempts:
2 left
💡 Hint
Check the first condition carefully with both parts.
✗ Incorrect
x is 7 > 5 and y is 5 < 10, so first if condition is true, printing "Yes".
❓ Predict Output
expert2:00remaining
If statement with assignment expression
What is the output of this code?
Python
if (n := 4) > 3: print(n * 2) else: print(n / 2)
Attempts:
2 left
💡 Hint
The walrus operator assigns and returns the value in the condition.
✗ Incorrect
n is assigned 4, which is greater than 3, so it prints 4 * 2 = 8.