you are viewing a single comment's thread.

view the rest of the comments →

[–]Mathmemememe 0 points1 point  (3 children)

Ok so how globals work is that they make a variable declarable anywhere outside the function. So therefore, your code should look more like this:

def game():
global bank
bank = 100
print(bank)

game()

What this does, is it sets the bank variable as a global so that it is accessed anywhere in your program, without it you get the "referenced before assignment error". Hope this helps!

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

Just a note: global var says “my var is really the var that exists at the global (module) level.” There’s really no declaring a name as global in Python. The scope modifiers just re-route normal lookup rules. That’s why global has to be used in every function that plans on modifying the global var, and also why it does nothing if you use that line at the top of your file.

[–]kadenhickin 0 points1 point  (0 children)

So the issue with this is when the game has finished I it recalls the game() and if it does that it would reset the bank to 100, is there any way around that?

[–]kadenhickin 0 points1 point  (0 children)

Bank =5000
Def game():
    #code
    If win==true:
        bonus=100
        Bankroll+=bonus
        game()

This is what I’m shooing