so, consider the following code: Note: indentation is a greater than sign:
def my_fun(a):
>a = a + 1
>x = a
>print(a)
>print(x)
x = 3
my_fun(x)
So I now have a question. If you run this, you will be printed a value of 4 4. Now, here's the fun part. If you type in x in the interpreter it will say, clearly, 3, but wait. I had a statement in the my_fun function that should have set x to 4, so why is it 3? Ferther more, if you type in a, the following will appear:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
but but but... I clearly set a to 4! So why, why in the world does the stupid thing reset and acts as though I have never defined a variable called a? After all, I did do it. So my question is how? How can I change the variable outside of a function instead of declaring a variable that lasts only till a function finishes running? Is it something to do with the return statement? I'd be really grateful for any help and would also appreciate if you provided a code snippid or some such.