0

The function below gives me this error:

"UnboundLocalError: local variable 'housebank' referenced before assignment"

def placeBet(table, playerDictionary, bet, wager):
    playerDictionary['You'][1].add(bet)
    housebank -= (wager*table[bet][0])
    table[bet][1]['You']=wager

The housebank variable is declared in my main function below:

def main():
    housebank = 1000000
    table = {'7' : [9/1,{}]}
    playerDirectory = {'player1':[1,set(),True]}
    placeBet(table,playerDirectory, 10, 100)

How can I use housebank in the placeBet function? If I do a return it will exit the main function, which I do not want to do... Any ideas?

1 Answer 1

1

housebank is local to placeBet. There's three ways to do it that I can see:

  • Make a class.

    class Foo:
        def __init__():
            self.housebank = 1000000
        def run():
            # ....
        def placeBet(....):
            # ....
            self.housebank -= (wager*table[bet][0])
            # ....
    
    def main():
        Foo().run()
    
  • Declare housebank in a wider scope:

    housebank = 1000000
    def placeBet(....):
        # ....
    def main():
        # ....
    
  • Make placeBet a closure inside main:

    def main():
        housebank = 1000000
        def placeBet(....):
            # ....
        # .... rest of main ....
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.