Python coin flip game of chance

Hi there I am currently trying to complete the coin flip game of chance but the code I have made does not seem to work and I cannot figure it out, any help would be greatly appreciated! thanks in advance!

import random

money = 100
num = random.randint(1, 10)
#Write your game of chance functions here
def coin_flip(guess, bet):
  flip = random.randint(1, 2)
  if flip == 1:
    flip = "Heads"
  elif flip == 2:
    flip = "Tails"
  if guess == 1:
      print("Well done, you have won" + str(money*2) + "congratulations!")
  elif guess == 2:
    print("Well done, you have won" + str(money*2) + "congratulations!")
  elif guess != 1:
    print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")
  else:
     print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")

Perhaps you could elaborate. What does or doesn’t your program do that it shouldn’t or should? How does what happens differ from what you expect/want?

1 Like

Well I’m expecting it to simulate a toin coss to return one of these string values, however it keeps saying that guess is not defined, I’m not sure where to go from here

The code you posted doesn’t include any calls to the function. When you call the function, are you supplying arguments for the parameters, guess and bet?

No, I didn’t think I would have to as I included a random integer generator?

Can you paste how a function call looks for this?

sure it is coin_flip(guess, bet)

This assigns a random integer to the variable num, but it isn’t used. The function requires 2 parameters, so when you call the function, you must provide arguments:

coin_flip(1, 50) #guess would be assigned to 1, and bet would be assigned to 50

You could use your randomly generated int num as an argument, but unless you have a 10-sided coin, you probably only want 2 options.

Then we have a few other issues to work out, but lets start with a proper function call. Post all of you code again with a function call at the end, and we can go on from there.

2 Likes
import random

money = 100
num = random.randint(1, 10)
#Write your game of chance functions here
def coin_flip(guess, bet):
  flip = random.randint(1, 2)
  if flip == 1:
    flip = "Heads"
  elif flip == 2:
    flip = "Tails"
  if guess == 1:
      print("Well done, you have won" + str(money*2) + "congratulations!")
  elif guess == 2:
    print("Well done, you have won" + str(money*2) + "congratulations!")
  elif guess != 1:
    print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")
  else:
     print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")
#Call your game of chance functions here
coin_flip(2, 30)

I have a few questions, because it’s difficult for me to see the goal in mind. What determines whether the user wins or loses? Is it 1? Is it 2? Is it “Heads”? Is it “Tails”? If you are going to determine win or loss over numbers, why do you have this code:

if flip == 1:
flip = “Heads”
elif flip == 2:
flip = “Tails”

Why do you have this line of code? num = random.randint(1, 10)? Is it related to the coin_flip function?

The losses within the code were defined by the not equals elif and else statements.

elif guess != 1:

print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")

else:

 print("Unlucky, you have guessed wrong, you have unfortunately lost" + str(money/2) + "better luck next time!")

and the win statements were determined by the equals if and elif statements.

f guess == 1:

  print("Well done, you have won" + str(money*2) + "congratulations!")

elif guess == 2:

print("Well done, you have won" + str(money*2) + "congratulations!")

So if guess is 2 do you win or lose?

elif guess != 1:
#guess being equal to 2 (which is not 1) means you lose
elif guess == 2:
#guess being equal to 2 means you win

What is the purpose of flip?
Your code contradicts itself way too much.

Maybe we need to take a step back. What do you want this function to do?

We’re neglecting to note that random.choice is a viable option so we don’t need to evaluate it…

 coin_toss = random.choice(['Heads', 'Tails'])
3 Likes

As @h1lo, pointed out, this line:

seems completely unrelated to your code.

Also, how does the following determine if the guess results in winning?

You probably mean to compare guess and flip rather than guess and either 1 or 2.

1 Like

So ultimately I want the function to simulate a coin flip where the user will guess either heads or tails and bet money on that chance aswell, whether he wins or loses will determine whether the bet is won or lost.

coin_flip('heads', 30)

Good. So what do you think needs to be done in order for this to work? @mtf mentioned a great option.

So:

if coin_toss.lower() == guess: #I used .lower() but if you want the first letter to be capitalized that's fine
  print('you win!')
#etc...

Yes I now do see where you are coming from with that, the num = random.randint(1, 10) the random 1-10 int generator was simply a step that I had to include in order to get to the next stage of the project.

So I would get rid of the numbers as they are overcomplicating my code and go over my else statements again to make sure they are not contradicting themselves

You only need a single comparison to determine if the guess is the same as flip. Either they are equal, and the player wins, or they aren’t, and the player loses.

Okay, I will revise my code and repost then see what you guys think, thankyou

2 Likes