So im working on a challenge in the Python 3 course.
The task requires creating a coin-flip simulator, where the user can bet by stating their guess and their bet, then the function runs and returns a statement of heads/tails and the earnings/losses. The code I wrote works fine for this, and instead of simply stating what they lost/won I added/subtracted from their original money.
I wanted to go a bit further and have it update the money variable outside the function, out of curiosity. How would I go about doing that? As it stands, it restarts after each game, reusing the initial money value of 100.
import random money = 100 Heads = 1 Tails = 2 #Write your game of chance functions here def coin_flip(guess, bet): flip = random.randint(1, 2) if flip == 1: new_money = (money + bet) print("Heads. You just won " + str(bet) + " dollars. Your total money is " + str(new_money) + " dollars remaining.") else: loss = -1 * (bet) new_money = (money + loss) print("Tails. You just lost " + str(bet) + " dollars. Your total money is " + str(new_money) + " dollars remaining.") #Call your game of chance functions here coin_flip(Heads, 6)
I was wondering if it had something to do with returning the values during the IF statement, but having only started Python on Wednesday, I’m rather confused.