Variable Scope Leak

Hard ⏱ 18 min 37% acceptance ★★★★★ 4.7
Write a function count_even(numbers) that counts how many values in the list numbers are even, using a local counter variable that does not leak into or depend on any global state. Then write a second function get_last_count() that always returns 0 (representing that no global counter exists) — this demonstrates that count_even's internal counter is properly scoped and never visible outside the function.

Examples

Example 1
Input
count_even([1, 2, 3, 4, 5, 6])
Output
3
Explanation

2, 4, 6 are even.

Example 2
Input
get_last_count()
Output
0
Explanation

No global counter variable exists or is touched.

Constraints

  • Do not declare or mutate any global/module-level variable from inside count_even.
  • numbers is a list of ints, possibly empty.

Topics

Variables & Data Typesscope

Companies

MicrosoftAtlassian

Hints

Hint 1

Declare `count = 0` inside the function body, not at module level.

Hint 2

`get_last_count` simply returns a hardcoded 0 to prove no shared state exists.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.