Python Game of Chance

Hi Guys,

Sorry for what’s likely something basic I’m missing here but I’ve come unstuck on the very 1st project (hopefully I’m not alone).

I’ve tried various ways of amending the code to get it to print what I want and can’t seem to get it to work.

I’ve imported it below but I basically want the programme to print out the users guess and stake, how much they won i.e. the bet and then their new total.

Any guidance on this would be massively appreciated, I’ve been sitting staring at the laptop for some time now trying to unpick it!

PS I’ve only just finished module 3 so I’m quite early in to the course…

import random
num = random.randint(1, 2)
money = 100

#Write your game of chance functions here

def coin_flip(guess, bet):
  if guess == "Heads" and num == 1:
    return (money + bet)
  elif guess == "Tails" and num == 2:
    return (money + bet)
  else:
    return (money - bet)
  return print("Your guess was " + guess + " and your stake was " + str(num) + " . You won " + str(bet) + "!" + " Your new total is " + str(money))
    
#Call your game of chance functions here

coin_flip("Heads", 50)

print and return do two very different things, i don’t think i need to explain print?

anyway, return hands back data to the caller (the function call), lets do a little analogy. You can perfectly return/hand back the test to your professor without filling in all the questions.

Same with the return keyword in python, a function is completed when a return keyword is reached (just like your done with the exam when you turn it in)

4 Likes

Hi Stetim,

Thanks for the reply and guidance, so are the return functions in each of the if / else statements correct to be in there then, as they are expecting to return a value e.g. money + bet or money - bet?

Would it look something like this (this still doesn’t seem to work):

import random
num = random.randint(1, 2)
money = 100

#Write your game of chance functions here

def coin_flip(guess, bet):
  if guess == "Heads" and num == 1:
    return (money + bet)
  elif guess == "Tails" and num == 2:
    return (money + bet)
  else:
    return (money - bet)
  
  print("Your guess was " + guess + " and your stake was " + str(num) + " . You won " + str(bet) + "!" + " Your new total is " + str(money))
    
#Call your game of chance functions here

coin_flip("Heads", 50)
1 Like

so the print statement is never reached, the function has reached a return keyword before that and thus the function exit.

2 Likes