# Cho han dice game help

Trying to figure out how to define odd and even numbers without having to type excess code. This is what I have so far. I know it’s probably wrong in all sorts of ways, but I am a total newb to this stuff.
Thanks!

``````def odd_num(dice):
if not num % 2:
return odd
def even_num(dice):
if num % 2:
return even

odd=odd_num
even=even_num

def cho_han(guess, bet):
dice = random.randint(1, 12)
if bet<1:
print("You must place a bet of at least \$1 to play")
elif bet>20:
print("You cannot bet more than the maximum bet")
elif guess == odd and dice == odd:
print("you bet \$" + str(bet))
print("Odd! you won " + str(bet) + "!")
print("Your new total is " + str(new_total))
elif guess == even and dice == even:
print("you bet \$" + str(bet))
print("Tails you won \$" + str(bet) + "!")
print("Your new total is " + str(new_total))
elif guess == even and dice==odd:
print("You bet, \$" + str(bet))
print("Odd, you lost, \$" + str(bet) + " better luck next time")
print("Your new total is \$"+ str(lost_total))
elif guess == odd and dice == even:
print("You bet, \$" + str(bet))
print("Tails, you lost, \$" + str(bet) + " better luck next time")
print("Your new total is \$"+ str(lost_total))

#Call your game of chance functions here
cho_han(even, 0)
``````

put

`````````
``````

Before and after code to tell the forum not to apply formatting rules to it.

Perhaps you’re looking for equal mod 2

``````> eq = (==) `on` (`mod` 2)
> eq 3 4
False
> eq 3 5
True
``````

Or maybe simply normalize them before comparing them with regular `==`

Or test whether the result comes out the same

``````> odd 3 == odd 5
True
``````

Is that a t12 dice or two t6’s? If it’s the latter then that isn’t an even distribution and `randint(1, 12)` wouldn’t be right. Can’t roll a 1 with two t6’s either.

3 Likes

That’s how new I am I don’t even know how to format the code in the forum lol.

I didn’t realize that with the dice. So, I should use `randint(2, 12)` to simulate 2 d6 dice?

``````def odd_num(dice):
if not num % 2:
return odd
def even_num(dice):
if num % 2:
return even

odd=odd_num
even=even_num
``````

^would this work if I returned a different result? or is there some other way of using if statements to define odd and even dice rolls? or are you saying to do?

`````` def odd_num(dice):
if not num == % 2:
return odd
``````

With `randint(2, 12)` every possible outcome (2, 3, 4, 5, …, 11, 12) will be equally probable.

This is not a good representation of 2 x d6 roll. It should be a lot easier to get 5 than 12, right? In the case of two six-sided dices, the possible outcomes are not equally possible.

3 Likes

So, I should do?

``````dice1 = randint(1, 6)
dice2 = randint(1, 6)
``````
2 Likes

Yes, that’s much better! 3 Likes

I don’t think you need the helper functions.

Assume that there are two and only two possible outcomes:

``````# okay
if (condition):
outcome = outcome_1
else:
outcome = outcome_2

# alternatively, this works
outcome =  outcome_2
if (condition):
outcome = outcome_1

# also, fun thing called "ternary operator"
outcome = outcome_1 if condition else outcome_2

``````

What does it all mean? Here’s an example: Program to allow a student to guess if a character is a vowel or consonant. This conflates some of the things above, but I think you’ll get the idea.:

``````def v_or_c(guess, ch):
if (ch in 'aeiou'):
outcome = "vowel"
else:
outcome = "consonant"
return "win" if guess == outcome else "lose"

print(v_or_c("vowel", "g"))
print(v_or_c("consonant", "x"))
# lose
# win

``````
2 Likes

Oh it’s not two numbers.

It’s not so much the comparisons that are repeated but these 4 lines over and over

``````    elif guess == odd and dice == odd:
print("you bet \$" + str(bet))
print("Odd! you won " + str(bet) + "!")
print("Your new total is " + str(new_total))
elif guess == even and dice == even:
print("you bet \$" + str(bet))
print("Tails you won \$" + str(bet) + "!")
print("Your new total is " + str(new_total))
elif guess == even and dice == odd:
print("You bet, \$" + str(bet))
print("Odd, you lost, \$" + str(bet) + " better luck next time")
print("Your new total is \$" + str(lost_total))
elif guess == odd and dice == even:
print("You bet, \$" + str(bet))
print("Tails, you lost, \$" + str(bet) + " better luck next time")
print("Your new total is \$" + str(lost_total))
``````

Keep only this:

``````    print("You bet, \$" + str(bet))
print("Tails, you lost, \$" + str(bet) + " better luck next time")
print("Your new total is \$" + str(lost_total))
``````

The middle one is different, there are two cases for that. Not four though.

If you’re using a recent version of python, you can format your strings like this instead:

``````  f"You bet, \${bet}"
^           ^   ^
|           # put python expressions in curly braces
# prefix the string with f
``````
2 Likes

I figured it out thanks to your guys’ help! Way less code than I anticipated.
Thanks!

``````def cho_han(guess, bet):
lost_total = money - bet
new_total = money + bet
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
num = dice1 + dice2
if bet<1:
print("You must place a bet of at least \$1 to play")
elif bet>20:
print("You cannot bet more than the maximum bet")
elif not num % 2 and guess == "odd":
print("you bet \$" + str(bet))
print("Odd! you won \$" + str(bet) + "!")
print("Your new total is \$" + str(new_total))
elif num % 2 and guess == "even":
print("you bet \$" + str(bet))
print("Even, you won \$" + str(bet) + "!")
print("Your new total is \$" + str(new_total))
else:
print("You bet, \$" + str(bet))
print("Odd, you lost \$" + str(bet) + ", better luck next time")
print("Your new total is \$"+ str(lost_total))
``````

What does `%` do? What is the result of that expression for an even number, and for an odd number?

Also, I see two cases for odd and one for Even, that’s a bit weird, isn’t it? Is it not possible to lose when choosing even?

This part is always exactly the same, does it need to be repeated 3 times?

1 Like