You're not modifying the variable, rather creating a new num1
in the function's namespace. If you want to reference the outer variable you'd need to put global num1
inside of the suma
function definition.
>>> num1 = '1'
>>> def suma():
... num1 = 45345
...
>>> suma()
>>> num1
'1'
>>> def suma():
... global num1
... num1 = 4234
...
>>> suma()
>>> num1
4234
This answer is the best on SO I've seen around global variables: https://stackoverflow.com/a/423596/5180047
Just realized the comment also answered what I've already said above, whoops! I'll leave this here anyways. Hopefully it's helpful.