Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Python: UnBoundLocalError

+1 vote
asked Oct 16, 2022 by Eidnoxon (5,140 points)
closed Oct 16, 2022 by Eidnoxon
Can somebody tell me what's wrong in my code?

https://onlinegdb.com/95QJywGpng
closed with the note: Found the answer (Thanks xDELLx)

1 Answer

+2 votes
answered Oct 16, 2022 by xDELLx (10,500 points)

print ('Hello World')
x = 1
wood = 0
coal = 0
    
def found():
    x=0
    x=x+1
    global wood
    wood+=15
    global coal
    coal+=15
    
found()


The variables wood & coal look ambigious to python.

Wood & coal in func "found" are by default treated as local varibles.

use global keyword to fix.

commented Oct 16, 2022 by Eidnoxon (5,140 points)
Thank you so much I've been working so hard and searched so much websites but never found the answer. You don't know how much this means to me. Now I can continue my project! Thanks!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...