I though I was fully aware of how LEGB rule worked until I came across this. Essentially I am to access the "total" variable within my function local namespace. I know the "total variable is a local namespace but for whatever reason when I try to run this function I get the following error: UnboundLocalError: local variable 'total' referenced before assignment
Example:
total = 0
def two_digit_sum(n):
for i in str(n):
total += int(i)
return total
number = 111
print(two_digit_sum(number))
However when I utilize the following function , I do not come across the error Example:
def spam():
print(eggs)
eggs = 42
spam()
^^^ As you can see the eggs variable is also a global function.