Games of Chance Code Challenge link: https://www.codecademy.com/practice/projects/games-of-chance

okay, so my code is:

import random

money = 100

#Write your game of chance functions here
def coin_flip(call, amount):
  
  if random.randint(1,2) == 1:
    coin_toss = "Heads"
  else:
    coin_toss = "Tails"
  if call == coin_toss:
    result = "won"
    total_amount = money + amount
  else:
    result = "lost"
    total_amount = money - amount
  
  print(
    "You flipped a %s and you called %s! You %s and now have $%.2f"
    %(coin_toss, call, result, total_amount)
  )
  
coin_flip("Heads", 10)

and this actually works quite nicely, however I am struggling to figure out how to update the global variable “money” to represent the new amount.

I’ve tried changing the code so that instead of “total_amount = money + amount” it shows “money = money + amount” in the hopes that it would update the global variable but instead it only returns an error message stating “the local variable money has been referenced before assignment”.

Any general thoughts on the code? How do I incorporate into my function the ability to continually update the global variable “money”?

@jacobgarwin295317990, you need to keep the game “alive” between runs, something that I don’t think is possible in the Codecademy interface, as it will not accept user input (i.e., how much do you want to bet each time around?)

If you have an IDE, either local or in-browser, in which you can ask for and accept user input (that would be just about any Python IDE), you can keep the game alive via a while loop. For more discussion, and an example, see here.

2 Likes

Using techniques you are already familiar with you could do something like this:

Spoiler
import random

money = 100

#Write your game of chance functions here
def coin_flip(call, amount):
  
  if random.randint(1,2) == 1:
    coin_toss = "Heads"
  else:
    coin_toss = "Tails"
  if call == coin_toss:
    result = "won"
    total_amount = money + amount
  else:
    result = "lost"
    total_amount = money - amount
  
  print(
    "You flipped a %s and you called %s! You %s and now have $%.2f"
    %(coin_toss, call, result, total_amount)
  )
  return total_amount #return the updated amount after each turn
  
money = coin_flip("Heads", 10) #assign the return value to money
money = coin_flip("Heads", 20)
money = coin_flip("Tails", 10)
money = coin_flip("Heads", 50)
money = coin_flip("Heads", 10)
print(money) #see the final value

You could also do this, but you may not have seen it before:

Alternate Spoiler
import random

money = 100

#Write your game of chance functions here
def coin_flip(call, amount):
  global money #this allows you to change the value of your globally scoped variable
  if random.randint(1,2) == 1:
    coin_toss = "Heads"
  else:
    coin_toss = "Tails"
  if call == coin_toss:
    result = "won"
    money += amount #update the value of money
  else:
    result = "lost"
    money -= amount #update the value of money
  
  print(
    "You flipped a %s and you called %s! You %s and now have $%.2f"
    %(coin_toss, call, result, money) #see the updated value
  )

  
coin_flip("Heads", 10)
coin_flip("Heads", 20)
coin_flip("Tails", 10)
coin_flip("Heads", 50)
coin_flip("Heads", 10)
print(money) #confirm the value of the global variable was changed

As @patrickd314 said, the game would be much more realistic if could be ‘live.’

1 Like

For completeness’ sake, I’ll note that the dreaded "UnboundLocalError: local variable 'x' referenced before assignment" is nicely explained in the docs.

1 Like