def sum(point):
point += 2 # local variable
return point
point = 10 # Global variables
n = sum(5)
print(n)
It won't work like this :
point = 4
def sum(name):
point = point + 2
return point
point = 10
n = sum(10)
print(n)
There is an error :UnboundLocalError: local variable ‘point’ referenced before assignment. Translation is : local variable point Quoted before assignment . in other words :python hold point = point + 2 Medium point Used as a local variable , However, he was not assigned a value , Therefore, an error is reported , Why local variables don't use global variables point Well ? This is because in the same row of assignment statements , Local variables cannot reference global variables , Will be python It is misunderstood as modifying the global variable value .