 # Games of chance: money value

I’d like the value of money to change based on the results of the coin_flip function, how do I achieve that? Below is the current code

``````import random

money = 100

#Write your game of chance functions here
def coin_flip(guess, bet):
print("Thank you for playing, you have bet " + str(bet) + " pounds on " + str(guess) + "!")
num = random.randint(1, 2)
if num == 1 and guess == "Heads":
new_total = money + bet
print("You win " + str(bet) + " pounds! You now have a total of " + str(new_total)+ " pounds!")
return new_total
if num == 2 and guess == "Tails":
new_total = money + bet
print("You win " + str(bet) + " pounds! You now have a total of " + str(new_total)+ " pounds!")
return new_total
else:
new_total = money - bet
print("You lost " + str(bet) + " pounds, your total has been reduced to " + str(new_total) + " pounds. better luck next time!")
return new_total
``````
1 Like

Hi!
Thanks for sharing your code. In the future make sure to use the </> button for readability.

Let’s say you’ve written your functions perfectly till now, you have them returning a number. Literally just returning. If it’s not printed, or used in some other capacity, it will do nothing.

So you have to decide where you want the effect to take place, or if your goal of that function is really to return a value (and not say, change an existing variable).

2 Likes

Thanks for the response

Im having trouble understanding as I haven’t got that far in the python course, what should I change about my code

1 Like

consider the differences of

``````money = 100

def do_something(money):
change = #some operation that changes money
money = change

#outside of method
print(money)

``````

vs

``````money = 100

def do_something_else(money):
change = #some operation that changes money
return change

#outside of the method
print(do_something_else(money))
print(money)

``````

Notice that in both examples, actually printing whatever change happens to money is different than changing money. Meaning: without printing, if you call a function to change a variable ‘money’, that variable will change (printing just displays the resultant value)

Ask yourself, what is the difference in these examples, and when could one be more useful than the other?

2 Likes

Thanks for your response, Its really helped

I have a new problem, when I print money it now appears 100 more than It should e.g when I use
`money += coin_flip("Tails", 10)` the money will be 210 if it wins or 190 if it loses whereas it should be 110 if it wins and 90 if it loses
The code is the same as before

No problem, any time!

Demonstrably not, as in your original you’re not capturing (or doing anything otherwise) with the return values from your function.

If you’ve fixed your previous issue, by adding additional code to capture the changes to the value of `money`, please re-post your code so we can see the current program.

Also, indentation is critical to Python; please make sure you format your code correctly. There is a guide on how to do this here: How do I format code in my posts?

1 Like

This is the current code

``````import random
money = 100

#Write your game of chance functions here
def coin_flip(guess, bet):
print("Thank you for playing coin flip, you have bet " + str(bet) + " pounds on " + str(guess) + "!")
num = random.randint(1, 2)
if num == 1 and guess == "Heads":
new_total = money + bet
print("You win " + str(bet) + " pounds! You now have a total of " + str(new_total)+ " pounds")
return new_total
if num == 2 and guess == "Tails":
new_total = money + bet
print("You win " + str(bet) + " pounds! You now have a total of " + str(new_total)+ " pounds!")
return new_total
else:
new_total = money - bet
print("You lost " + str(bet) + " pounds, your total has been reduced to " + str(new_total) + " pounds. better luck next time!")
return new_total

money += coin_flip("Tails", 10)
``````

You’re doing `+=` with the return value, when you should just be doing `=`.

You’re taking account of the wager inside the function, and `new_total` adjusts the contents of the “wallet” accordingly. By then doing `+=` with the return value, you’re saying to add the value of `new_total` to the existing value of `money`.

``````new_total = 120 # I won!
money += new_total # = 100 + 120 = 220.
``````

Hello, I also have a question about this exercise. I want to let you choose only one word in input: heads or tails. how to achieve this? Thank you in advance

``````#game of chances

import random

money = 100
tails = 0

def flip_coin():
flip = random.randint(0, 1)
bet = int(input("How much you want to bet (max 100) >>>> "))
win = money + bet
loose = money - bet
choice = (input("Choose heads or tails >>>> "))

if (flip == 1) and (put == heads):
return "You won: " + str(bet) + "\$ " + "Now you have: " + str(win) + "\$"

elif (flip == 1) and (put == tails):
return "You won: " + str(bet) + "\$ " + "Now you have: " + str(win) + "\$"

elif (flip == 0) and (put == heads):
return "You loose: " + str(bet) + "\$ " + "Now you have: " + str(loose) + "\$"

elif (flip == 0) and (put == tails):
return "You loose: " + str(bet) + "\$ " + "Now you have: " + str(loose) + "\$"

else:
return "Error: Choose heads or tails"

print(flip_coin())
``````

It shouldn’t matter what the user inputs. If it doesn’t match the coin toss, the user loses. That’s all the validation we need.

1 Like

You can use an `if`, or sometimes better a `while` to check that the users input is `==` to the valid options:

``````answer = ""
There are a couple other ways to do this, but hopefully this gives you some ideas 