Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1global score | ALets the function assign to the score variable defined outside all functions | |
| 2print(score) | Bdef add_point(score): return score + 1 | |
| 3Declare a variable as global before changing it inside a function | CWithout global, score = 0 creates a new local variable and the outer score stays 5. Add global score inside reset() so the outer variable is changed. | |
| 4Passing a parameter and returning the result is safer than changing a global | Ddef add_point(): global score score = score + 1 | |
| 5score = 0 def add_point(): score = score + 1 | EReads a global variable inside a function without needing the global keyword | |
| 6def reset(): score = 0 score = 5 reset() | FThe global keyword is missing, so Python treats score as local and raises an UnboundLocalError when it is read before assignment. Add global score as the first line inside the function. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
global — A keyword used inside a function so it can assign to a variable defined outside all functions, instead of creating a new local variable with the same name.def bump():
global score
score += 1def reset():
global score
score = 0lives = 3
def show():
print(lives)try:
add()
except UnboundLocalError:def bump(score):
return score + 1
score = bump(score)