Python Games of Chance

def coin_Flip(guess,bet):

  coin = random.randint(1, 2)

  if coin == 1:

    coin = "Heads"

  if coin == 2:

    coin = "Tails"

  if guess == coin:

    money += bet

  else:

    money -= bet

coin_Flip("Tails",50)

print(money)

Why does this not work?

Hey there and welcome to the forums!!

Currently your function coin_Flip() is attempting to modify a variable money. Do you have this variable defined any were? I’m guessing this isn’t all of your code since you don’t have random imported. Also have you learned about scope yet? A variable is only accessible to a limited portion of a program unless declared as a global. Even if you have money defined outside your function, your function can’t currently access it.


Besides the point but worthwhile to mention is that you don’t need this if:

if coin == 2:

Since if coin doesn’t equal 1, it can’t equal anything else.

1 Like

Here is all my code. Thank you for confirming that my function will only return 1 or 2.

import random

money = 100

#Write your game of chance functions here

def coin_Flip(guess,bet):

coin = random.randint(1, 2)

if coin == 1:

coin = "Heads"

if coin == 2:

coin = "Tails"

if guess == coin:

money += bet

else:

money -= bet

coin_Flip(“Tails”,50)

print(money)

Your problem is in the scope of you money variable. Recall that a functions variable are isolated from the rest of a program, and the only values it has access to are ones it declared:

def function():
  variable = "value" 
  # This variable is declared inside the function

Or the values passed to it as arguments:

def function(num):
  # code here

variable = "value"
function(variable) # calling function and passing a variable to it

However even if you pass a value to a function, the function can’t permanently modify it as it really only copies the variable to work with it.

A couple options for you to modify your money variable are to declare it as global within the function:

variable = "value"

def change_variable():
  global variable  # include variable as a global
  variable = "new value"

Though I would not recommend this option as global variables can often get messy and hard to work with. Another would be to pass money as an argument and then return it:

variable = 5

def change_variable(var_to_change):
  var_to_change += 5   # Modify variable
  return var_to_change # return it

variable = change_variable(variable)
# and assign it to variable outside of function

Yet another option could be, if instead of modifying money in the function, what if you return the positive or negative value of bet and add that to money? Now your function does not need to have money passed to it, and you can still avoid the use of a global:

variable = 5

def subtract(num):
  return -num  #return negative value of num

variable += subtract(5)
# and add it to variable

It sort of worked, it will not change “money” every time and sometimes jumps from 50 to 150, or vice versa.
import random

money = 100

#Write your game of chance functions here

def coin_Flip(money,guess,bet):

coin = random.randint(1, 2)

if coin == 1:

coin = "Heads"

else:

coin = "Tails"

if guess == coin:

money += bet

else:

money -= bet

return money

money = coin_Flip(money,“Tails”,50)

print(money)

1 Like

Sorry I just saw your edit.

Your code is working fine for me, money is always either changed from 100 to 150, or 100 to 150. What are you expecting it to do?

Based on what you said:

sometimes jumps from 50 to 150

are you expecting it to save the modified value of money every time you run the program? If that is the case, variables are not saved when the program terminates. So it doesn’t matter what money is at the end of a program, running it again will reset it to 100, which is why you only get 50 or 150.

Eventually you will learn about how to read and write from files and then you can save a variable to be used again later.


In the future it might be good to relook at formatting your code. Currently you are only formatting chunks of it. To format everything, simply press this button once:

Then copy/paste all of your code between the two rows of backticks:

2 Likes

Sorry, I forgot that I am not running it and saving the amount in “money”.

I made it so that after winning or losing 50 I either double my money or lose all my money:
import random

money = 100

#Write your game of chance functions here

def coin_Flip(money,guess,bet):

coin = random.randint(1, 2)

if coin == 1:

coin = "Heads"

else:

coin = "Tails"

if guess == coin:

money += bet

else:

money -= bet

return money

money = coin_Flip(money,“Tails”,50)

print(money)

money = coin_Flip(money,“Tails”,money)

print(money)

1 Like

By the way, what is your favorite 8-bit game?

Very nice! I don’t know if you’ve gotten to loops yet, but once you do you could refactor your code so you can keep playing based on user input:


That’s a tough one :joy:
I like a lot of the classics like Mario, Pacman, and Megaman. As for my favorite I’m torn between Galaga and Dig Dug.

1 Like