I have a question that is sort of a problem.
I am making a crafting program to learn python more but came across a problem; I assign my variables at the start and user them later like you are supposed to, however it came up with an error message 'UnboundLocalError: local variable 'vSaplings' referenced before assignment'. I tried many different ways of writing it but in the end, all I had to do was change
vSaplings
to
v_Saplings
Now there is no error. Why did this need to be done? It's just a character being added.
Code in question:
vSaplings = 20
...
elif reqO == "Twigs":
print("This requires at least 1 sapling.")
if vSaplings > 0:
amountSaplings = input("How many saplings would you like to use to craft twigs? 1 sapling = 3 twigs.")
This gets the error. Changing 'vSaplings' to 'v_Saplings' removes the error. Why?
Thank you.
EDIT: I now understand that it actually needs to be a global variable. Just 1 more question then, do I need to make my variables global in every single function I create? That seems like it would take up a lot of space.