Complete the code to print the value of the outer variable inside the inner function.
def outer(): x = 10 def inner(): print([1]) inner() outer()
The inner function can access the variable x from the enclosing scope.
Complete the code to modify the outer variable inside the inner function using the correct keyword.
def outer(): count = 0 def inner(): [1] count count += 1 print(count) inner() inner() outer()
global instead of nonlocal.The nonlocal keyword allows the inner function to modify the variable count from the enclosing scope.
Fix the error in the code by completing the blank with the correct keyword to modify the outer variable.
def counter(): num = 5 def increment(): [1] num num += 2 return num return increment() print(counter())
global which causes an error here.The nonlocal keyword is needed to modify num inside increment which is in the enclosing scope.
Fill both blanks to create a closure that remembers the value of start and adds step each time.
def make_adder(start): step = 0 def adder(): nonlocal [1] [2] += 1 return start + [2] return adder add = make_adder(5) print(add()) print(add())
start instead of step.nonlocal.The variable step should be declared nonlocal and incremented inside adder to remember its value.
Fill all three blanks to create a function that counts calls using an enclosing variable and returns the count.
def call_counter(): count = 0 def counter(): [1] count count [2] 1 return [3] return counter c = call_counter() print(c()) print(c())
global instead of nonlocal.Use nonlocal to modify count, increment it with +=, and return count.