Games of Chance Confusion

Hi all,

First post here, i’m having a bit of trouble when testing the heads or tails game that i’ve written for the Python 3 module. https://www.codecademy.com/practice/projects/games-of-chance

Code below, but basically when I run the function, it will go through every step when I win, but when I lose, it will skip the final else statement it seems? Any tips would be great as i’m a bit lost!

import random
money = 100

#Write your game of chance functions here
def coin_toss(bet, guess):
  #Need to ensure that the bet is higher than 0 first
  if bet <= 0:
    print("Your bet needs to exceed 0 to play.")
    return 0
  
  #Flip the coin
  print("Okay, let's flip! You guessed " + guess)
  coin_result = random.randint(0,1)
  
  #Show the result to the user?
  if coin_result == 0:
    print("Heads!")
  elif coin_result == 1:
    print("Tails!")
    
    #Calculates the result
    if (coin_result == 0 and guess == "Heads") or (coin_result == 1 and guess == "Tails"):
      print("You win $" + str(bet) + ", well done!")
      return bet
    else:
      print("Better luck next time! You lost $" + str(bet) + ".")
      return -bet
	
  





#Call your game of chance functions here
coin_toss(5, guess = "Tails")
1 Like

I believe this is because your indentation is not correct.

the part that calculates the result only looks to be working if the result is tails because it is indented that way. and you get the win result because you test your function on tails.

coin_toss(5, guess = "Heads")
this however would never calculate the results cause that part is only accessible when it lands tails.

2 Likes

I don’t get this project at all in the first place. In your code-

import random
money = 100

#Write your game of chance functions here
def coin_toss(bet, guess):
  #Need to ensure that the bet is higher than 0 first
  if bet <= 0:
    print("Your bet needs to exceed 0 to play.")
    return 0

Why did you return zero in the function coin_toss()? I don’t get the reason for it.

Because the solution is structured like this:

import random

total = 100

def coin_flip(guess, bet):
    # coin_flip code here

def higher_card(bet):
    # higher_card code here

def cho_han(guess, bet):
     # cho_han code here

def roulette(guess, bet):
    # roulette code here

total += coin_flip("Heads", 10)
total += higher_card(5)
total += cho_han("Even", 2)
total += roulette("Even", 10)
total += roulette(3, 1)
total += roulette("Odd", total)
print("Your total amount of money is " + str(total))

… so that all of the total += function_call() statements at the bottom run sequentially; if one of the bets is bad (i.e., < = 0), the expression is still expecting a number. If it instead receives None or a string like “Bad bet”, the script will halt with a TypeError. The way it is written, it will continue despite the “bad bet.”

HI CODERS,
First time posting in a discussion
I Dont Know Whether My code is right or not

import random

money = 100

#Write your game of chance functions here
def coin_flip(guess,bet):
num = random.randint(1,2)
money = 100
if bet <= money:

if num == 1 and guess == “HEADS”:
money += bet
print(“outcome = HEADS” +" Player won " + str(bet))
print(“AVAILABLE BALANCE :” + str(money))
elif num == 2 and guess == “TAILS”:
money += bet
print(“outcome = TAILS” +" Player won " + str(bet))
print(“AVAILABLE BALANCE :” + str(money))
elif num == 1 and guess == “Tails”:
money -= bet
print(“outcome = HEADS” +" Player lose " + str(bet))
print(“AVAILABLE BALANCE :” + str(money))
elif num == 2 and guess == “HEADS”:
money -= bet
print(“outcome = TAILS” +" Player lose " +str(bet))
print(“AVAILABLE BALANCE :” + str(money))
else:
print(“INSUFFIECIENT BALANCE”)

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

Next time you post, you should format your code correctly. You can do this by clicking the button that looks like this: </>

What’s in this loop?

Are you sure this is the right way to subtract?
I’m not sure I can troubleshoot much more without the code being properly formatted.