#'Local variable gold referenced before assignment'
31 messages · Page 1 of 1 (latest)
Did you initialize the variable gold 🤔
Like set it to a value?
You redefined gold inside the function. If you want to assign to a global variable from within a function you've gotta add global gold inside your function.
That's a line all to itself. Typical to put the global variable lines at the beginning of the function
def my_function():
global some_variable
some_variable = "some value"
aight okay thanks, i'll do that now
def gold():
global gold
gold = int(10)
would that be okay?
or does the function thing need to be something different>
I think name of function should be different
But i think they meant to add global gold to your existing function
oh okay i'll plug that in
wait and every time i use gold do i use the function name or the variable name?
Gold is a variable. If you want the value of the variable use the variable name. If you want to use the function use the function name.
ok, thanks big dog
so for the line that's breaking everything i'd use global gold, right?
Try it and see
it says invalid syntax
Please show the code that gives the error
The line with the global keyword needs to be exactly how I showed. You do not use the global keyword any other way.
It happens once in the function to tell the Python interpreter that your function will need to use that global variable at some point.
ohhh sorry yeah i get what you mean now