 # Looking for your thoughts on how this code could be shortened

I just finished the Cho Han game in the games of chance project. It works perfectly as far as I can tell, but I know I must be missing something because I think there is way too much code here.

import random

money = 100
#Write your game of chance functions here
#Cho_Han
def cho_han(guess, bet):
if bet == 0:
print(“You need to place a bet to play.”)
return 0
else:
print("Your guess is " + str(guess) + “. Let’s roll the dice!”)

roll_result_1 = random.randint(1,6)
roll_result_2 = random.randint(1,6)
turn_result = roll_result_1 + roll_result_2
print(turn_result)
if turn_result == 3:
print(“Odd!”)
elif turn_result == 5:
print(“Odd!”)
elif turn_result == 7:
print(“Odd!”)
elif turn_result == 9:
print(“Odd!”)
elif turn_result == 11:
print(“Odd!”)
else:
print(“Even!”)

if(turn_result == 3 and guess == “Odd”) or (turn_result == 2 and guess == “Even”):
print("Nice! You won " + str(bet)+ "!") return bet elif(turn_result == 5 and guess == "Odd") or (turn_result == 4 and guess == "Even"): print("Nice! You won " + str(bet)+ “!”)
return bet
elif(turn_result == 7 and guess == “Odd”) or (turn_result == 6 and guess == “Even”):
print("Nice! You won " + str(bet)+ "!") return bet elif(turn_result == 9 and guess == "Odd") or (turn_result == 8 and guess == "Even"): print("Nice! You won " + str(bet)+ “!”)
return bet
elif(turn_result == 11 and guess == “Odd”) or (turn_result == 10 and guess == “Even”):
print("Nice! You won " + str(bet)+ "!") return bet elif(turn_result == 12 and guess == "Even"): print("Nice! You won " + str(bet)+ “!”)
return bet
else:
print(“You lost \$” + str(bet) + “. Try again! You’re luck is bound to improve.” )
return -bet

cho_han(“Odd”, 10)

Looking for any advice on how I could have made this run with less code.

Rather than writing code for every possibility, you could make one case for odd and one for even since all odd share outcome, and all even share outcome.

Being odd is a property that can be tested. What does an odd number do that an even does not? If a number does that, then you’ve determined that it’s odd.

1 Like

Thank you! revised code based on your feedack.

import random

money = 100
#Write your game of chance functions here
#Cho_Han
def cho_han(guess, bet):
if bet == 0:
print(“You need to place a bet to play.”)
return 0
else:
print("Your guess is " + str(guess) + “. Let’s roll the dice!”)

roll_result_1 = random.randint(1,6)
roll_result_2 = random.randint(1,6)
turn_result = roll_result_1 + roll_result_2
print(turn_result)
if turn_result % 2 == 0:
print(“Even”)
else:
print(“Odd”)

if(turn_result % 2 == 0 and guess == “Even”) or (turn_result % 2 != 0 and guess == “Odd”):
print("Nice! You won " + str(bet)+ "!") return bet else: print("You lost " + str(bet) + “. Try again! You’re luck is bound to improve.” )
return -bet

#Call your game of chance functions here
cho_han(“Odd”, 10)

you can do the same thing with that.
test if the outcome is the same, rather than test if both are even or if both are odd

… oh the user inputs even/odd… never mind.

You could have something like:

`remainderGuess = 1`
or
`remainderGuess = 0`

and then `if (turn_result % 2 == remainderGuess)`

…it might end up less readable and therefore worse.

Like this, I suppose, is what I’d go for:

``````roll = ...
outcome = 'odd' if roll % 2 else 'even'
if outcome == guess:
win
else:
lose``````

I don’t really get your logic, here’s a suggestion for finding odd numbers (will work in this range of turn_result)