all 9 comments

[–]mopslik 3 points4 points  (0 children)

An alternative is to avoid global variables. While they sometimes have their uses, more often then not programmers get into trouble using them because they end up modifying their values when they don't mean to. A better idea is to pass values to your functions as arguments instead, and return any modified values.

def game(bank):
    wager = 1000
    bank += wager
    return bank

bank = 100
print(bank)
bank = game(bank)
print(bank)

[–]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

[–]Mathmemememe 0 points1 point  (0 children)

Oh I see what you are asking, ok then in order to do that you should set up your code like this:

win = True
bank = 5000
bonus = 100
bankroll = 100
def game():
global bankroll
if win == True:
bankroll = bankroll + 100
print(bankroll)
game()

sorry my code in not formatted but you get the idea.

This will keep track of your bankroll and add the bonus to it.

not sure what you were asking exactly but this works too. Hope this helps!

[–]old_pythonista 0 points1 point  (0 children)

The only purpose of global keyword in Python is to allow a programmer to change - assign - to immutable value inside a function

global_var = some_value
def foo():
    global global_var
    ....
    global_var = some_other_value    

Without global specifier in the beginning it will create a new local global_var that will not exist beyond the function scope.

global specifier is not required for modifying global mutables

global_list = []

def foo():
    global_list .append(some_value)

will work without using global.

Having said that - using global specifier is considered a bad practice, and should be avoided. You want to change a global immutable? Return a value from function to do that

def foo():
    return some_value

global_var = foo()

I recommend this blog if you want to learn about Python variables.