Games of Chance Coin Flip always returns losing result

With the ‘Games of Chance’ project, I’ve done what I believe is right but every time I run the code it returns the result for a losing guess. If it’s a random return then at least some of the results should be winning results. That leads me to wonder if I’ve done something wrong with setting up my code, or if the its something else.

https://www.codecademy.com/practice/projects/games-of-chance

I’ve searched the forum, but haven’t found anything that helps me figure this out. Any help would be much appreciated.

1 Like

The following code has some lines that won’t do what you think they do.

 flip = random.randint(1, 2)
 1 == "Heads"
 2 == "Tails"

1 == "Heads" is a truthy statement that is just asking to evaluate if 1 is equal to ‘heads’.
Not only is the statement always going to be false, it’s also just going to disappear because it is not assigned.

Some examples:

true_or_false = 1==2
print(true_or_false)
# Output: False

true_or_false = 1==1
print(true_or_false)
# Output: True

true_or_false = 1=='1'
print(true_or_false)
# Output: False

One thing you can think about is setting a conditional to let your flip variable take your desired value depending on the number of the flip.

Also consider making your random numbers between 0 and 1, since 0 will automatically be false if evaluated by an if-statement, and 1 true.

print(bool(0))
# output: False
print(bool(1))
# output: True

if 0:
   print('hi')
# no output

if 1:
   print('hi')
# output: hi

Additionally note that in your win conditions, the string says the player has won or lost x money but that’s only happening in a string (and nowhere else in the code). Consider having a player_1_wallet etc. or something to that extent.

1 Like

@toastedpitabread Thanks for the help. If you don’t mind, I’m going to try to make sure I really understand what you’re saying by repeating some of it back how I understand it. Also, I do have a question that I’m hoping you can help expound on.
So, from what you said:

  • List item 0 is always false and 1 is always true when using Booleans, so they work really well when trying to differentiate between two things.
    • List item I’m assuming it’s more that 0 is always false and any other number is true. Is this correct, or is it strictly that 0 is false and 1 is true?
  • List item == is only going to be used with True/False
  1. That part I get, I actually started out trying to have “Heads” = 1 and when that didn’t work I tried 1 = “Heads” (and the Tails equivalent). I ended up with syntax errors for both of those which is when I tried ==. I honestly didn’t think == would work because I was pretty sure it only applied to Booleans, but when I got an output that wasn’t an error I just went with it. I should have thought that out better.

  2. List item If I’m understanding you correctly, it’s actually my use of == that is resulting in me always returning the losing message result? This is because the statement is never going to be true while it’s set up using ==?

  • List item If I want to keep track of the money won/loss, I need to create another function that will handle that aspect of the game.
  1. List item I originally tried including money += (bet * 2) in the win return and the opposite in the lose return, but that didn’t work. Did that not work because of my misuse of Booleans? Could I include the output of money won/loss in the original function, or will that not work because it’s only contained in the function so it won’t continue outside of the function?

I’m very new to coding as a whole and trying to make sure I really understand what I learn, as opposed to just memorizing things. Thanks again for all your help and feedback. It’s going to help me cement in these concepts.

clearly I’m also new to forums since my bullet points include “List item”, sorry about that :roll_eyes:

No problem, welcome to the forums!

Yes, usually if you say if x, as long as x exists, it will evaluate as True. In this case semantically I thought it made sense consider how bits are 1s and 0s.


List item If I’m understanding you correctly, it’s actually my use of == that is resulting in me always returning the losing message result? This is because the statement is never going to be true while it’s set up using ==?

Not really. What I’m saying is that these statements don’t result in either a value being modified or a value being printed. The overall action is that the computer will evaluate them, but you won’t know about it for all it’s worth.

 1 == "Heads"
# no output. 1 remains the same, "Heads" remains the same.

@dickykitchenjr One way to do this (but not the only way)

flip = random.randint(1, 2)
if flip == 1:
   flip = "heads"
else:
   flip = "tails

List item If I want to keep track of the money won/loss, I need to create another function that will handle that aspect of the game.

Not necessarily. In a started program, you have have variables like you did with money = 100.

The only change is that your win condition would not only return a string, but also a modification to player_1 score.

In a slightly more involved program you would probably have player class. But that’s a different topic.

1 Like

FTR

In most programming languages, 0 is falsy. Any other number is truthy.

In Python, the numeric equivalent of the two boolean primitives is,

False  =>  0
True   =>  1
>>> int(False)
0
>>> int(True)
1
>>> float(False)
0.0
>>> float(True)
1.0
>>> 
2 Likes

Thanks for the clarification. Yes.

2 Likes

Thanks @mtf and @toastedpitabread I really appreciate the help and clarification.

1 Like

This is what I ended up doing, in case anyone else comes across this problem. I still haven’t added a way to create a running tally of earning and losses, but it addresses the original problem listed in the start of the post…

1 Like