I want to count the number of calls of the function f
inside of inc
. How should I modify the function main
to do it?
I have this code:
def inc(f):
f()
f()
def main():
a = 0
def f():
a += 1
inc(f)
print(a) # should print 2
main()
But it leads to the error:
Traceback (most recent call last):
File "main.py", line 11, in <module>
main()
File "main.py", line 9, in main
inc(f)
File "main.py", line 2, in inc
f()
File "main.py", line 8, in f
a += 1
UnboundLocalError: local variable 'a' referenced before assignment