Updating the money variable

I’ve been working on the games of chance project today and feel really good about my COIN FLIP game. I’m struggling with figuring out how to updated the money variable as the game is played.

https://www.codecademy.com/practice/projects/games-of-chance

import random

money = 100
#Write your game of chance functions here

COIN FLIP

def coin_flip(guess, bet):
coin_flip = random.randint(0,1)

if coin_flip == 0:
coin_flip = “Heads”
else:
coin_flip = “Tails”

if bet == 0:
print(“You must place a bet to play!”)

else:
print(“Call it in the air!”)

if (guess == “Heads” and coin_flip == “Heads”) or (guess == “Tails” and coin_flip == “Tails”):
print("You called: " + str(guess))
print("The coin landed on: " + str(coin_flip))
print(“Congratulations! You won the flip!”)
print("You won " + str(bet)) print("Your remaining balance is " + str(money))
return money + bet

else:
print("You called: " + str(guess))
print("The coin landed on: " + str(coin_flip))
print(“You lost. Too bad! Try again.”)
print("You lost " + str(-bet)) print("Your remaining balance is " + str(money))
return money - bet

#Call your game of chance functions here
coin_flip(“Tails”, 10)

1 Like

money = coin_flip("Tails",10)
Your money will either go up or down by 10

1 Like

+=, probably, to add the difference

@sewy44 try putting ``` (three backticks) before and after your code, like so:

```
some
  code
here
```
2 Likes

I tried this. It doesn’t actually update the money amount. I’m looking for a way to update the balance of money after each turn. I think it has something to do with +=, but I haven’t cracked it yet.

2 Likes

Are you trying to preserve money through successive gameplays? If so, that will take a bit more coding to keep the function alive after each play.

2 Likes

That’s exactly what I’m trying to figure out. Any suggestions? Here’s my current code. I think I’m getting closer.
https://www.codecademy.com/practice/projects/games-of-chance

import random
money = 100

COIN FLIP GAME

def coin_flip(guess, bet):
coin_flip = random.randint(0,1)

if coin_flip == 0:
coin_flip = “Heads”
else:
coin_flip = “Tails”

if bet <= 0:
print(“You must place a bet to play!”)
else:
print(“Call it in the air!”)

if (guess == “Heads” and coin_flip == “Heads”) or (guess == “Tails” and coin_flip == “Tails”):
print("You called: " + str(guess))
print("The coin landed on: " + str(coin_flip))
print("Congratulations! You won " + str(bet) + "!") return(bet) else: print("You called: " + str(guess)) print("The coin landed on: " + str(coin_flip)) print("You lost. Too bad! Try again. You lost " + str(bet) + “.”)
return(-bet)

money += coin_flip(“Tails”, 10)
print(“Your remaining balance is $” + str(money))

1 Like

Take a look at this thread.

1 Like
import random

money = 100
#Write your game of chance functions here

#COIN FLIP
def coin_flip(guess, bet):
  coin_flip = random.randint(0,1)

  if coin_flip == 0:
    coin_flip = "Heads"
  else:
    coin_flip = "Tails"
  if (guess == "Heads" and coin_flip == "Heads") or (guess == "Tails" and coin_flip == "Tails"):
    return money + bet

  else:
    return money - bet

#Call your game of chance functions here
print money
money = coin_flip("Tails", 10)
print money

No += because of the functions return values

@sewy44
I cut out all the extraneous stuff so you can see the logic here.
Run this code in an editor and you will see your moneys original value of 100 and then your money after a bet.

1 Like

… or, change the return values to bet and -bet, and money += coin_flip() works fine

# prior code unchanged
    return  bet
  else:
    return  - bet
#Call your game of chance functions here
print money
money += coin_flip("Tails", 10)
print money

Output:

100
90

The problem with trying to make this this exercise more interesting is that the function halts and all variables are lost after just one run.

This could be easily fixed using a while loop and a "play another?? (y/n)" type of query, but the Codecademy interface as manifested in this lesson will not accept input() (or raw_input()), so in order to code a script that actually allows you to play all of the games as often as you like while maintaining a running bank, you must use a different IDE.

1 Like

I like to be able to assume that a function’s result is based only on the arguments I provide, unless it specifically needs to do otherwise! (as is the case here with obtaining randomness, but nothing else needs to be magical about it)

# can't do this if coin_flip relies on a global value
balances = [100, 17, 1000, 80, 3]
updated_balances = [balance + coin_flip('tails', 10) for balance in balances]
# this would be fine too, but coin_flip doesn't really need to know
updated_balances = [coin_flip('tails', 10, balance) for balance in balances]
1 Like