Clemcy
Clemcy

Reputation: 1

Problem about the scope of a global variable

I'm starting to develop in python and I have a problem with the scope of one of my variables

I declare a variable called "bankroll" of integer type and assign it a value of 500 I then use it in a function and pass it as a parameter but at the end of the execution of my function its value remains unchanged I do not understand (the function is defined in another module but is used in the same file or is declared "bankroll")

I tried to return this variable from my function and reassign it to bankroll but its value does not change

# the function taking in parameter bankroll
def roulette(mise,numeroChoisis, bankroll, continuerPartit):

   else:
        print("c'est perdu !")
        bankroll - mise
        math.ceil(bankroll)
        return bankroll

#the main 
bankroll = rouletteGain.roulette(mise, numeroChoisis, bankroll, continuerPartit)

Upvotes: 0

Views: 30

Answers (1)

Christian Legge
Christian Legge

Reputation: 1035

You have to declare a variable as global in your function in order for it to work as one. Like this:

def increment(val):
    global x
    x = x + 1

x = 4
increment(x)
print(x) # 5

Upvotes: 1

Related Questions