logo

Question detail

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.

Try the question, check the answer, then read the explanation to understand the curriculum point.

At a glance

Question

Type

practice

Style

Topic

Programming

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.

Related flashcards

No flashcards are published for this page yet.

Related practice questions

No questions are published for this page yet.