0
0
Pythonprogramming~20 mins

Python Block Structure and Indentation - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Python Indentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-else with indentation
What is the output of this Python code snippet?
Python
x = 5
if x > 3:
    if x < 10:
        print("A")
    else:
        print("B")
else:
    print("C")
AB
BA
CC
DNo output
Attempts:
2 left
💡 Hint
Check which conditions are true and how indentation groups the print statements.
Predict Output
intermediate
2:00remaining
Effect of wrong indentation on loop execution
What will be printed by this code?
Python
for i in range(3):
    print(i)
  print('Done')
AIndentationError
BSyntaxError
C
0
1
2
Done
D
0
1
2
Attempts:
2 left
💡 Hint
Look carefully at the indentation of the last print statement.
Predict Output
advanced
2:00remaining
Output of function with mixed indentation
What is the output of this code?
Python
def f():
    x = 1
    if x == 1:
        print('Yes')
     print('No')
f()
AIndentationError
B
Yes
No
CYes
DNo
Attempts:
2 left
💡 Hint
Check the indentation of the second print inside the function.
Predict Output
advanced
2:00remaining
Output of code with multiple blocks and else
What will this code print?
Python
for i in range(2):
    if i == 0:
        print('Zero')
    else:
        print('Not zero')
    print('Loop done')
A
Zero
Loop done
Not zero
B
Zero
Not zero
Loop done
Loop done
C
Zero
Not zero
Loop done
D
Zero
Loop done
Not zero
Loop done
Attempts:
2 left
💡 Hint
Notice the indentation of the last print statement inside the loop.
🧠 Conceptual
expert
3:00remaining
Identifying the cause of unexpected output due to indentation
Consider this code: x = 10 if x > 5: print('Big') if x > 8: print('Very big') else: print('Small') Why does it print only "Big" and "Very big" and never "Small"?
ABecause Python ignores else blocks after nested ifs.
BBecause the else belongs to the second if, which is true, so else is skipped.
CBecause the else belongs to the first if, and x > 5 is true, so else is skipped.
DBecause indentation is wrong and else is outside any if.
Attempts:
2 left
💡 Hint
Check which if the else is paired with based on indentation.