Exam-style question
Try this first
Write a Python 3 subroutine called calculate_area that declares a local variable called area, assigns it the product of length and width, and returns area. Then explain why area is a local variable.
Answer and explanation
Concise answer
def calculate_area(length, width): area = length * width return area
Why this is correct
The assignment area = length * width declares and assigns the variable within the subroutine. Its scope is limited to calculate_area, and it does not remain available as a local variable after the subroutine finishes.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
