 Cho-Han over 9000

This took me a few hours of refining, but I’m really glad I did it. This is hands down the cleanest code I’ve ever wrote. I’m sure it can be done much better, but it’s one of those times where I impressed myself (you all know those moments).

import random

money = 100

def cho_han(money, bet, call):
di1, di2 = random.randint(1,6), random.randint(1,6)
sum = di1 + di2
won = 'You Win'

if sum % 2==0 and call == 'Cho':
money += bet
elif sum % 2==1 and call == 'Han':
money += bet
else:
won = 'You lose'; money -= bet

print(f"The sum is: {sum}. You bet \${bet} on {call}. {won}! Total Money: \${money}.")

cho_han(money, 20, 'Han')
cho_han(money, 50, 'Cho')
cho_han(money, 70, 'Cho')
cho_han(money, 99, 'Han')
cho_han(money, 15, 'Cho')

The sum is: 11. You bet \$20 on Han. You Win! Total Money: \$120.
The sum is: 7. You bet \$50 on Cho. You lose! Total Money: \$50.
The sum is: 8. You bet \$70 on Cho. You Win! Total Money: \$170.
The sum is: 8. You bet \$99 on Han. You lose! Total Money: \$1.
The sum is: 6. You bet \$15 on Cho. You Win! Total Money: \$115.

2 Likes

Looks pretty good, but isn’t the value assigned to money supposed to be a running total? If you start at 100 and win 20 that’s 120. Then you lose 50, so money should be 70, and so on…

Thanks for the catch! Here’s the updated version.

import random

money = int(input('How much money would you like to start with? \$'))

def cho_han(money, bet, call):
if call == 'cho' or call == 'han':
# Should ask user each round, but for testing: run until money = 0
while money > 0:
di1, di2 = random.randint(1,6), random.randint(1,6)
sum = di1 + di2
won = 'You Win'

if sum % 2==0 and call == 'cho':
money += bet
elif sum % 2==1 and call == 'han':
money += bet
else:
won = 'You lose'; money -= bet

print(f"The sum is: {sum}. You bet \${bet} on {call.capitalize()}. {won}! Total Money: \${money}.")
else:
print("Invalid selection. Try again.")

bet = int(input("How much would you like to wager? \$"))
call = input("What's your call? (Cho/Han) ").lower()

cho_han(money, bet, call)