Games of Chance - Help with a variable

Can’t seem to update my money variable.
It keeps getting from the 100 each time.
I don’t want to look in the solution right now, but need some help on this part.

You won 10 money
110

You won 10 money
110

You won 10 money
110

You lose 10 money
90

CODE:

import random

#Write your game of chance functions here
money = 100

def coin(call, bet):
  flip = random.choice(['Heads', 'Tails'])
  global money 
  if flip == call:
    money += bet
    return 'You won {bet} money!!'.format(bet = bet)
  else: 
    return 'You lost {bet} money :('.format(bet = bet)
    money -= bet
  
  
print(coin('Heads', 10))
print(money)

Hello, @thomasgallasso179783, and welcome to the forums!

Your reported output doesn’t match what your code does. I’m guessing you just posted similar examples to your actual output. If I run your code 3 times, I get the following output:

You lost 10 money :(
100
You lost 10 money :(
100
You won 10 money!!
110

How about 5 times?

You lost 10 money :(
100
You lost 10 money :(
100
You won 10 money!!
110
You lost 10 money :(
110
You lost 10 money :(
110

From these results, I can see that your code updates money if I win, but not if I lose. Look at your code, and see if there isn’t something different in how the two scenarios are handled. (There is.)

Also, please review How do I format code in my posts?
I edited your post above, so you can see the difference.

@midlindner Hey man… I’m posting in here for the first time, didn’t know how to format. Thanks a lot for that.
I’ll do that next time.

About the code, you were right.
It was not working on the losing bet.
But it was just because I’ve moved things around trying to fix the problem I mentioned on the topic.
Already fixed it haha thanks for that!

The problem is that the function is updating the variable, but it does it only once.
Why can’t it goes updating to 100, 90, 100, 110, 120, 130, 120… and so forth?

It looks like its reseting and always starting from 100.
Do you know what I mean?

1 Like

I know what you mean, but that isn’t what’s happening. When you win, the money variable is updated, when you lose, it is not. There is a specific reason this is happening. Notice the result from running your code 20 times:

import random

#Write your game of chance functions here
money = 100

def coin(call, bet):
  flip = random.choice(['Heads', 'Tails'])
  global money 
  if flip == call:
    money += bet #money is updated
    return 'You won {bet} money!!'.format(bet = bet) #return ends the function
  else: 
    return 'You lost {bet} money :('.format(bet = bet) #return ends the function
    money -= bet #return ended the function, so..

#run 20 times  
for i in range(20):  
  print(coin('Heads', 10))
  print(money)
Output:

You lost 10 money :frowning:
100
You lost 10 money :frowning:
100
You won 10 money!!
110
You lost 10 money :frowning:
110
You lost 10 money :frowning:
110
You lost 10 money :frowning:
110
You lost 10 money :frowning:
110
You lost 10 money :frowning:
110
You lost 10 money :frowning:
110
You won 10 money!!
120
You won 10 money!!
130
You lost 10 money :frowning:
130
You won 10 money!!
140
You won 10 money!!
150
You lost 10 money :frowning:
150
You won 10 money!!
160
You lost 10 money :frowning:
160
You won 10 money!!
170
You won 10 money!!
180
You lost 10 money :frowning:
180

You can see that money is updated every time you win, but never when you lose. Check out the comments I added to your code.

After re-reading your edited post, I think I understand what is happening. money is assigned 100 each time you click Run in the learning environment. You need to call the function multiple times in your code, like I did in my previous post to see that money is updated.

1 Like

Awesome man! Just did the loop and it worked.
Thanks a lot.
Sorry for the misunderstanding.

2 Likes

No worries, and you’re welcome! Happy coding!

1 Like