0
0
Pythonprogramming~10 mins

Python Block Structure and Indentation - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

What will Python raise if the print line is not indented inside the function?

Python
def greet():
print("Hello")
# Python raises: [1]
Drag options to blanks, or click blank then click option'
AIndentationError
BSyntaxError
CNameError
DTypeError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Confusing IndentationError with SyntaxError β€” IndentationError is a subclass of SyntaxError but more specific.
Thinking missing indentation causes a runtime error instead of a parse-time error.
2fill in blank
medium

How many spaces does Python's PEP 8 standard recommend per indentation level?

Python
# PEP 8 recommends [1] spaces per indent level
def example():
    if True:
        pass  # this line is 2 levels deep
Drag options to blanks, or click blank then click option'
A2
B4
C8
D1
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 2 spaces β€” common in other languages but not Python's standard.
Mixing tabs and spaces which causes IndentationError.
3fill in blank
hard

What does this code print? Pay close attention to which print belongs inside the loop.

Python
for i in range(3):
    print("A")
print("B")
# Total lines printed: [1]
Drag options to blanks, or click blank then click option'
A3
B1
C6
D4
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking both prints are inside the loop β€” only the indented one is.
Forgetting that the unindented print runs once after the loop.
4fill in blank
hard

Fill both blanks. The return statement's indentation decides when the function returns.

Python
def add_until(n):
    total = 0
    for i in range(1, n+1):
        total += i
    return total

print(add_until(3))
# Output: [1]
# return belongs to the [2] block
Drag options to blanks, or click blank then click option'
A6
B1
Cfunction
Dloop
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking return is inside the loop β€” check its indentation level.
Confusing function-level and loop-level indentation.
5fill in blank
hard

Fill all three blanks. Determine each output line based on which block each print belongs to.

Python
x = 10
if x > 5:
    print("big")
    if x > 8:
        print("very big")
print("done")
# Line 1: [1]
# Line 2: [2]
# Line 3: [3]
Drag options to blanks, or click blank then click option'
Abig
Bvery big
Cdone
Dnothing
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking 'done' only prints when conditions are true β€” it is outside all if blocks.
Missing the nested if block β€” the second indent level creates a block inside a block.