Check your understanding of the previous reading: variable scope, local vs. global variables, and how a function’s return value, not the variable itself, is what becomes available outside it.

Question 1

True or False: The scope of a variable is how long the variable is "alive".

Select one answer, then check.

  • Correct. This matches the reading's definition: a variable's scope is how long it stays alive, visible, and available while the program runs.
  • Not quite. The reading defines a variable's scope exactly this way: as its lifetime, how long it stays available before it disappears.

Question 2

How many scopes are there in this program?

def make_message(value: int):
    return "The result is" + str(value)

def modify_number(a_number: int) -> int:
    return a_number * 5 + 3

start = 10
print(make_message(modify_number(start)))

Select one answer, then check.

  • Correct. The module level, where start is defined and the print call happens, is the program's one global scope. Calling modify_number and then make_message creates two separate local scopes, one per call.
  • Not quite. A program only ever has one global scope, the module level. The two function calls are what create local scopes, not global ones.
  • Not quite. There is only ever one global scope, the module level. What you may be counting are the two local scopes created by the two function calls.
  • Not quite. Only one scope in this program is global, the module level. Everything else happens either in that one global scope or inside the two local scopes from the function calls.
  • Not quite. There is only one global scope, and only two calls happen here, modify_number(start) and make_message(...), so only two local scopes are created.
  • Not quite. Only two functions are called here, so only two local scopes exist alongside the program's one global scope.

Question 3

Mark each of the following that are good rules of thumb:

Select every one that applies, then check.

  • Correct. A local variable only exists for the lifetime of its function call, so code outside that scope shouldn't rely on it.
  • Correct. Relying on a global variable from inside a function makes that function harder to reason about and reuse on its own.
  • Not quite. This inverts the actual rule: a local variable should be used inside its own scope, not exclusively outside it (and it isn't even available outside it, see the earlier question).
  • Not quite. This inverts the actual rule: a global variable isn't restricted to being used only inside one particular function's local scope.

Question 4

coupon = 2.99

def calculate_price(cost: float) -> float:
    tax = 1.1
    price = cost * tax - coupon
    return price

original_cost = 7.99
new_price = calculate_price(original_cost)
Identify each variable in the program above as Global or Local (local to calculate_price).
coupon
tax
cost
price
original_cost
new_price

Question 5

True or False: Return can be used to return a variable so that variable is available outside a local scope.

Select one answer, then check.

  • Correct. A function returns a value, not the variable itself: the local variable stops existing once the function ends. That value only stays available afterward if the caller assigns it to a variable of its own.
  • Not quite. What crosses back out of the function is the value the local variable held, not the local variable itself. It only stays around afterward if the caller assigns that value to a new variable.

Question 6

Consider this program:

1 | x = 5
2 | def do_math(x: int) -> int:
3 |     return (x ** x + x)
4 | do_math(x)

How many times is the variable x defined on line 1 used (either read or written) in this program, including on line 1 itself?

Show worked solution

The global x from line 1 is used twice:

  • Line 1: it is written, by the assignment x = 5.
  • Line 4: it is read, as the argument passed into do_math(x).

Line 2 introduces a parameter also named x. Inside the function body on line 3, that parameter shadows the global variable, so x ** x + x refers to the local parameter, not the global x from line 1.

Question 7

y = 0

def increase(y: int) -> int:
    y = y + 1
    return y

increase(y)
increase(y)
print(y)

Question 8

Why are global variables bad?

Select one answer, then check.

  • Correct. Relying on global variables means keeping track of multiple levels of scope at once, which is exactly what the reading points to as the problem with them.
  • Not quite. This isn't a memory-leak issue; the reading's concern with global variables is about how they complicate reasoning about the program, not about memory.
  • Not quite. The reading is clear that relying on global variables is a habit to avoid, for the reason the correct option describes.
  • Not quite. The reading's objection to global variables isn't about performance.

Question 9

def add_half(a_variable: int) -> float:
    a_variable = a_variable + .5
    return a_variable

a = 1
a = add_half(a)
print(a)

Question 10

def change_value(a_variable: int) -> int:
    a_variable = 5
    return a_variable

a = 7
change_value(a)
print(a)