Hello! I’ve run into an error in this project and I cant seem to figure out of to make it work. I was wondering if any of you could help me. Whenever I run this function, it gives me an error saying that the ‘money’ variable was referenced before assignment. Here is the code I wrote:

import random

money = 100

#Write your game of chance functions here
def coin_flip(guess, bet):
Tails = random.randint(1,1000)
win = “Not Defined”

win = “Tails”

if guess == win:
money += bet
msg = “You win \$” + str(bet) + “!”
return msg

elif guess != win:
money = money - bet
msg1 = “You lost \$” + str(bet) + “! Sorry!”
return msg1

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

Thanks for all the help! I really appreciate it!

1 Like

I’m not sure, I’m fairly new to this myself… The only difference I see is during a ‘win’, you used the the += operator to change the value of money, whereas during the a ‘loss’, you used money = money - bet

1 Like

Or maybe you need to return the value of money from the function? So it goes back to the global value?

1 Like

Oh! good idea! I’ll try that and report back!

1 Like

Hmm, even after I added `return money` it still pulled up the same error. I think theres a way to make a variable inside a function “Global” so i can use it outside the variable. Maybe i try that?

1 Like

I added a code inside the function that goes like this:

``````global money
``````

and now it works! Thanks for the help!

1 Like

awesome!!! not a problem!! I think you could also do something like have your function return the amount that a player bet… (negative if they lost)

and then call your function like this:

``````money += function()
``````
1 Like

Wow! Thats a pretty good idea! I’ll try that out next time I run into a problem like this! Again, thanks so much!

1 Like