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.
Model answer
What a good answer should say
- def calculate_area(length, width): area = length * width return area area is a local variable because it is declared inside calculate_area and is accessible only while that subroutine is executing.
- It exists only for the execution of that subroutine.
Explanation
Why this works
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.
Common mistake
No common mistake is linked to this question yet.
