Global Variable in Python
Level 397 keystrokes
This uses the global keyword to modify a variable defined outside the function scope.
Python code
# A module-level (global) variable1counter = 02 # A function that changes the global counter3def increment(): # Without global, counter += 1 would create a local variable and raise UnboundLocalError4 global counter # Adds 1 to the global counter5 counter += 16 # Two calls: counter goes 0 → 1 → 27increment()8increment()# Prints 29print(counter)Output
2
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
(× 4)× 4=× 2:× 1+× 1