Made a module in python. Which is acting as a model of machine code. I have a variable named accumulator.
As in
accumulator=0
However after I run a function that changes accumlator, as in
def function(number):
accumulator=number
return
and then print the accumulator to show what is in it, it just says 0.
How can I make a variable that will stay recognised even after the function creating it has finished?
It’s because
accumulatoris global and theaccumulator=numberinside the function is binding a new local nameaccumulatorequal to number. Withinfunctionyou could useaccumulatorbut you can’t assign to it without use of theglobalstatement.You will get the desired result using:
Note the return is redundant, as if a Python statement doesn’t return, it defaults to returning
None.