Forum Archive

UnboundLocalError

AtomBombed

Can someone tell me why I am getting an "UnboundLocalError" when the function is called? It's getting annoying because the error is being super annoying, and for me: hard to debug.

# coding: utf-8

VESP_HOLDERS = {"Sean":float(20),
                 "Spencer":float(20),
                 "Nathan":float(20)}
extra_pool = 60.0

def transact(price,buyer,seller):
    if hi == 2:
        print(hi)
    if extra_pool <= 0.0:
        if VESP_HOLDERS[buyer] - price <= 0:
            return False
        elif VESP_HOLDERS[buyer] - price >= 0:
            VESP_HOLDERS[buyer] -= price
            VESP_HOLDERS[seller] += price
            return True
    elif extra_pool > 0.0:
        extra_pool -= price/2
        EXTRA_HOLDERS[buyer] -= price
        EXTRA_HOLDERS[seller] += price + price/2
        return True

transact(20.0,"Sean","Spencer")
print(VESP_HOLDERS["Sean"]+"\n"+VESP_HOLDERS["Spencer"])
ccc

The basic idea is "Tell me what the value of 'hi' is before you ask me if hi == 2."

So add a line at the top of your script that says: hi = 1