 Hi.

My solution to the 4. Cho-Han game (www.codecademy.com/practice/projects/games-of-chance), contains errors as every time it is printing the else statement (loss), even if the prediction and the result match (win).

import random

money = 100

def cho_han(prediction, bet):

dice1 = random.randint(1, 6)

dice2 = random.randint(1, 6)

result = dice1 + dice2

print("The result is " + str(result) + “.”)

if result % 2 == 0:

``````result = "Even"

print("This number is even.")
``````

else:

``````result = "Odd"

print("This number is odd.")
``````

if(result == “prediction”):

``````new_balance = money + bet

print("You won! You bet " + str(bet) + ". Your new balance is " + str(new_balance) + ".")
``````

else:

``````new_balance = money - bet

print("You lost. You bet " + str(bet) + ". Unfortunately your new balance is " + str(new_balance) + ".")
``````

cho_han(“Even”, 25)

I would like to understand what I have done wrong. Is it because of my “result” parameter to which I attributed different values or the fact that I used 2 different if statements?

Hello @joannastroz270069404. Could you please click this button:

And put the code in-between the back ticks: As this will preserve indentation, etc.

Now, the problem might be this:

Specifically `"prediction"`, is it meant to be a string? What this `if` statement does is check if the `result` is equal to the string `prediction`. Is that what you want it to be doing? If not, how could you change it?

I hope this helps!

1 Like

@codeneutrino, thank you, I will paste my code in this way from now on.

Sorry, it was such a silly mistake… prediction obviously wasn’t a string. I have corrected it and it works now. Thank you so much!

``````import random
money = 100

def cho_han(prediction, bet):
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
result = dice1 + dice2
print("The result is " + str(result) + ".")
if result % 2 == 0:
result = "Even"
print("This number is even.")
else:
result = "Odd"
print("This number is odd.")

if(result == prediction):
new_balance = money + bet
print("You won! You bet " + str(bet) + ". Your new balance is " + str(new_balance) + ".")
else:
new_balance = money - bet
print("You lost. You bet " + str(bet) + ". Unfortunately your new balance is " + str(new_balance) + ".")

cho_han("Even", 25)
``````
2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.