Always getting same answer in Cho-Han game of chance quiz?

Hi,
Please help me figure out what’s wrong with my code. I always get the else statement printed every time! (the Quiz/Exercise)

Feel free to post your way (and probably better!) of writing the code but since I’m trying to learn, I really want to understand why and where my code is wrong as well? So I’m really looking for an explanation of why it’s always printing the same answer?
Thanks!

import random

money = 100


def game_cho_han():
    die_1 = random.randint(1, 6)
    die_2 = random.randint(1, 6)
    bet = (input("Please place your bet"))
    odd_or_even = (input("Odd or Even?"))
    if die_1 + die_2 == 3 or 5 or 7 or 9 or 11 and odd_or_even == "Odd":
        return "Congrats! " + str(die_1 + die_2) + " is Odd so you win " + str(bet) + "€!"
    else:
        return "Sorry! " + str(die_1 + die_2) + " is Even so you loose " + str(bet) + "€!"


Python has order of operations, so you should break down the if condition to see how its evaluated. You can write down how you think the if condition works and then we can analyze it together

this:

die_1 + die_2 == 3 or 5 or 7 or 9 or 11

make sense from a human perspective. For a computer, not so much. There is “famous” internet joke about it:

It illustrates how conditions work. For example, or 5, the 5 isn’t compared with anything, so python will just see if its a truthy value. (it is, positive integers are considered true)

Hi,
(I didn’t get the joke btw…clearly not a programmer yet :neutral_face:)

How I think the if condition works? Well, if something is True then execute the next line of code, if not, then move on…explained in my layman’s terms.
So I read between the lines and I think I got the implicit hint so I put some () here and there to group some operations and now I’m getting the opposite result, i.e. only the if statement is printed!

I can appreciate how you are trying to teach me by not giving me the answer but the risk is just frustration and simply giving up and moving on so could I kindly ask you to just tell me what I did wrong? Don’t worry, I’ll learn from it as well :wink:
Thx!

If you could provide the exercise url? Then i can see what the intention is, currently, i don’t know

what i meant is, what if take certain inputs, for example:

def game_cho_han():
    die_1 = 3
    die_2 = 4
    bet = 10
    odd_or_even = "Odd"

with these inputs, can you break down the order in which the if condition evaluates? So, to get started, + will happen first:

if 7 == 3 or 5 or 7 or 9 or 11 and odd_or_even == "Odd"

then the == resolve. Can you tell me how the rest is going to play out?

these analytic skills (breaking the code down) are essential to learn.

(((((7 == 3) or 5) or 7) or 9) or (11 and (odd_or_even == "Odd")))

Evaluate it. Left-to-right, innermost first.

If you could provide the exercise url?

Oh, sorry. I thought I had done that but I now see that in my OP I hyperlinked to the code and not the Quiz/Exercise, which I hope I have done correctly now! :wink:
Just wanted to get that out quickly, I’ll now review the rest of your message and the other poster as well…
Thanks!

That was horribly confusing…but I guess that’s maybe the point you’re trying to make, that I completely messed it up :wink:. Kind of knew that though.

No, those operators are very simple, that’s the point.
Or one point, anyway.
Another point is that you should know what they do. You should know what all things you use do. You should be able to argue for everything in your code, or if you can’t then it shouldn’t be there.

The behaviour you were hoping to get from them would require them to do a whole lot of things under the hood, something that they haven’t promised to do. So you cannot expect for it to do what you want.

OR looks at the thing on the left. If it’s truthy, then the result is the value on the left. Otherwise, the result is the value on the right.

^ that is what you need to be leveraging when you use or.

similarly, AND looks at the value on the left. If it’s truthy, then the result is the value on the right. Otherwise, the result is the value on the left.

Geez…thx again ionatan but I’m afraid you’re talking a bit above my head. It’s a bit too cryptic. I can see how you are trying to get me thinking about it to solve it myself but I’m just getting more an more muddled in confusion.
I would really appreciate something closer to the actual solution. :roll_eyes:

There isn’t one, the, solution.

You’d decide what should happen, and then describe that in code.

Having a solution without having decided what should happen… is a bit backwards methinks.

To add to @ionatan answer, this means start breaking down/analyzing your if condition, to see what is happening.

What does this do?

print(5 or 7)

see my description of OR. compare to how you’re using OR

or is an operator with some particular behaviour. to use it you’d need to know what it does. right?
what does or promise to do? that’s the only thing you can use it for, isn’t it?

As for coming up with a solution, that probably involves deciding to do something like … given some number, let’s call it x, compare x to 3, compare x to 5, compare x to 7, compare x to 9, compare x to 11
given those results, combine them in a way that if any of them compared as equal, you’d know. those results are booleans, and you want to combine them into a single boolean, do you know an operator with such behaviour?

and, how would you compare, how can you test whether two things are equal?
there’s an operator for that as well, ==

so you’d need to do all of these:

x == 3
x == 5
x == 7
x == 9
x == 11

And then you would have several booleans.

Or, if you’re looking specifically for odd numbers, then what does odd mean? odd is something that can be tested because odd numbers behave a certain way when divided by 2

We can use or, and it still worth learning about.

I inserted fixed values in your program:

def game_cho_han():
    die_1 = 4
    die_2 = 4
    bet = 10
    odd_or_even = "Even"
    total_of_dice = die_1 + die_2
    if total_of_dice % 2 == 0 and odd_or_even == "Even":
        return "Sorry! " + str(die_1 + die_2) + " is Even so you loose " + str(bet) + "€!"
    else:
        return "Congrats! " + str(die_1 + die_2) + " is Odd so you win " + str(bet) + "€!"


print(game_cho_han())

i went for even, and the dice result is even, yet i lost?

We can use or , and it still worth learning about.

I certainly do want to learn how to use or, I’m just not getting it! :pensive:

I know, I realised now there is nothing in there in case someone plays odd and wins.
I’ll work on it and be back…
(I’ll delete the previous post which would only add confusion to the thread)

Sigh…I need help.
I was about to roughly duplicate my if/return and else/return statements and just change the values to reflect a bet on Odd but I didn’t get very far as PyCharm was not happy with me and giving me an unreachable code error before I could even finish typing!

What am I doing wrong…again! :weary:
Hopefully a nested if function isn’t going to be necessary here because I’m not ready for that (that would really do my head in).

Throw me a bone please, I’m at breaking point.

To be fair to myself, maybe I’m going beyond the scope of the exercise? If you read what it says:

Create a function that simulates playing the game Cho-Han. The function should simulate rolling two dice and adding the results together. The player predicts whether the sum of those dice is odd or even and wins if their prediction is correct.
The function should have a parameter that allows for the player to guess whether the sum of the two dice is "Odd" or "Even" . The function should also have a parameter that allows the player to bet an amount of money on the game.

I wanted to have some ‘fun’ by introducing the input function and return print statements for 4 scenarios (win & loose for both Odd & Even) but maybe with the tools that I’ve been taught up to now in the Python 3 course, I’m not expected to be able to do it as I am without some additional knowledge?
What do you think?

Hello, @chumbolito.

Sometimes to move forward, we have to take a step back. Consider what your function must do. What are the steps? My take on the steps:
Steps:

  1. generate random int between 2 and 12 inclusive
  2. get user input:
    bet amount
    guess (even or odd)
  3. determine if random int is even or odd
  4. determine if user’s guess matches random int (even or odd)
  5. print you won or you lost depending on previous step (#4)

You’ve come close to implementing these steps. Consider what you actually need to know to determine whether the user has won or lost. Instead of comparing many possibilities using several if statements, you really only need one. You just need to know if the random int is even or odd, and if the user guessed correctly. What if you determine whether the random int is even or odd, and assign a value reflecting that to a variable:

if random_int % 2 == 0:
    even_odd = "Even"
else:
    even_odd = "Odd" 
#could use a ternary expression instead if you are familiar with those:
even_odd = "Odd" if random_int % 2 else "Even"
#random_int % 2 returns 1 or 0. If it returns 1 that is truthy, so "Odd" is assigned, otherwise "Even" is assigned

Then the user’s guess can be compared to that value. If it matches, they won. If not, they lost.

Thanks!
I gave it a shot with your suggestions but…same thing!

def game_cho_han():
    dice_roll = random.randint(2,12)
    bet = (input("Please place your bet"))
    odd_or_even = (input("Even or Odd?"))
    if dice_roll % 2 == 0:
        odd_or_even = "Even"
    else:
        odd_or_even = "Odd"
    if bet == odd_or_even:
        return "Dice roll was " +str(dice_roll) + " You win!"
    else:
        return "Dice roll was " +str(dice_roll) + " You lose"

Where did I go wrong?

What makes you say it’s wrong? That’s what you need to be following to its source

that’s not how two dice behave btw.
rolling 2-12 is not equivalent to rolling 1-6 twice and adding up the rolls

A good way to see what is going wrong is to print the values.

print(bet)
print(odd_or_even)
if bet == odd_or_even:

Are these the expected values?

Also, I hadn’t considered the change in behavior, but @ionatan is correct: