hi again guys.
Today i’m trying out functions, but i’ve run in a small (inconsistancy?) issue.
Mainly i’m trying to print out global variables within a function, which works,
but when I try to change the value of the pre-created variables, from inside the function, its not allowing me. I’ve pasted the error in the comment near the commands
a = 1
b = 2
string1 = ""
def test_print():
print (a) #- works fine
print (b) #- works fine
print(string1) #- works fine
#string1 += "hello" #- not working // error : referenced before assignment
#string1 = string1 + "hello" #- not working either // error : referenced before assignment
test_print()
If place string1 = “” inside the function the error goes, but thats not what I want, in case I’ll have a value in string1 which I do not want to overwrite.
thanks in adv
edit forgot to say that i did read about the error in the documentation which says that since theres no need to declare variables before hand, the assignment within a function makes the variable local? still didnt quite understand it completely why, and even if so, is it possible to use the same variable without losing whatever value is stored inside it?