Games of Chance

import random

money = 100

#Write your game of chance functions here
###Coin Flip Game Start
def coin_flip(bet, bet_amount):
  global money
  coin_flip_result = random.randint(1,2)
  if coin_flip_result == 1 and bet == 'Heads':
    money += bet_amount
    print('You got lucky! It was heads! You have won:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 2 and bet == 'Tails':
    money += bet_amount
    print('You got lucky! It was tails!You have won:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 1 and bet == 'Tails':       
    money -= bet_amount
    print('Unlucky! It was heads! You have lost:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 2 and bet == 'Heads':
    money -= bet_amount
    print('Unlucky! It was tails! You have lost:$', bet_amount, 'You now have:$', money)
#print(coin_flip('Tails', 20))
  
###Coin Flip Game End

I have two questions:

  1. If I don’t have ‘global money’ in the coin_flip function, I get error: UnboundLocalError: local variable ‘money’ referenced before assignment. Why is this? money has already been defined outside the function and should work, no?

  2. Every time I call the function money resets to 0, it doesn’t ‘remember’ the previous games where I’ve won or lost money. How do I fix this?

side question: when I tried to post this question I had to select 2 tags but the tags had to be predefined tags and I couldn’t find ones that really fit (hence why python-gradebook is a tag) what?

We can access global variable perfectly fine within the function:

a = 3

def example()
   print(a)

example()

see? That goes fine.

the problem is that we can’t change the value of the global variable without using global. Which is a good thing, it means we can’t accidentally update a global variable (which in larger programs is very nice)

the reason you get an UnboundLocalError is because you do assignment, python “assumes” the variable is local.

tried multiple function calls:

import random

money = 100

#Write your game of chance functions here
###Coin Flip Game Start
def coin_flip(bet, bet_amount):
  global money
  coin_flip_result = random.randint(1,2)
  if coin_flip_result == 1 and bet == 'Heads':
    money += bet_amount
    print('You got lucky! It was heads! You have won:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 2 and bet == 'Tails':
    money += bet_amount
    print('You got lucky! It was tails!You have won:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 1 and bet == 'Tails':       
    money -= bet_amount
    print('Unlucky! It was heads! You have lost:$', bet_amount, 'You now have: $', money)
  elif coin_flip_result == 2 and bet == 'Heads':
    money -= bet_amount
    print('Unlucky! It was tails! You have lost:$', bet_amount, 'You now have:$', money)

print(coin_flip('Tails', 20))
print(coin_flip('Tails', 20))
print(coin_flip('Tails', 20))
print(coin_flip('Tails', 20))

the money is updated

1 Like