Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 14, 2020 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/81dd53ac1796ea769b4a855d5b886248 to your computer and use it in GitHub Desktop.
Save codecademydev/81dd53ac1796ea769b4a855d5b886248 to your computer and use it in GitHub Desktop.
Codecademy export
import random
money = 100
#Write your game of chance functions here
def validate_bet (bet):
print ("You are betting: $", bet)
if bet > money:
print ("You don't have enough money make that bet")
return False
if bet <= 0:
print ("You need to bet money in order to play")
return False
return True
def flip_a_coin (bet, side):
if validate_bet(bet):
print ("You Choose: ", side)
print ("Fliping the coin...")
coin = random.randint(1,2)
if coin ==1:
print ("It's Heads!")
else:
print ("It's Tails!")
if coin == 1 and side =="Heads":
print ("You win!")
return bet
elif coin == 2 and side == "Tails":
print ("You win!")
return bet
else:
print ("You Lost")
return -bet
else:
return 0
def Cho_Han (bet, guess):
if validate_bet (bet):
print ("You Choose: ", guess)
print ("Rolling the dice...")
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice_sum = dice1 + dice2
print ("Dice#1:", dice1, "Dice#2:", dice2, "Sum:", dice_sum)
if ((dice_sum) % 2) == 0:
if guess == "Even":
print ("You win!")
return bet
else:
print ("You Lost")
return -bet
else:
if guess == "Odd":
print ("You win!")
return bet
else:
print ("You Lost")
return -bet
else:
return 0
def picking_card (bet):
if validate_bet (bet):
"""
Generating the whole card deck (52): 4 times a series from 1 to 13.
Numbers 11,12,13 represents Joker, Queen, King
Assume Ace value is always 1
"""
card_deck = [i for i in range(1,14)]*4
# picking the first card of the deck
card1 = card_deck.pop(random.randint(0,52))
# picking the second card of the deck
card2 = card_deck.pop(random.randint(0,51))
print ("Your card is:", card1)
print ("My card is:", card2)
# to show that the cards are different
# use this line -> print (card_deck)
if card1 < card2:
print ("You Lost")
return -bet
elif card1 > card2:
print ("You win!")
return bet
else:
print ("It's a Tie!")
return 0
else:
return 0
def roulette (bet, guess):
if validate_bet (bet):
print ("You are betting on:", guess)
print ("Spinning the wheel...")
# 00 is represented by 37
number = random.randint(0,37)
if number == 37:
print ("The winner number is: 00")
else:
print ("The winner number is:", number)
if number == 0 or number ==37:
print ("You Lost")
return -bet
elif guess == number:
print ("You win big!")
return bet * 35
elif ((number % 2) == 0 and guess == "Even") or ((number %2) == 1 and guess == "Odd"):
print ("You win!")
return bet
else:
print ("You Lost")
return -bet
else:
return 0
#Call your game of chance functions here
"""
# This is the test for flip_a_coin ()
for i in range (1,6):
print ("Playing FLip_a_Coin...")
decision = random.randint(1,2)
if decision == 1:
money += flip_a_coin (i*10,"Heads")
else:
money += flip_a_coin (i*10,"Tails")
print ("You have now: $", money)
"""
"""
# This is the test for Cho_Han ()
for i in range (1,6):
print ("Playing Cho_Han...")
decision = random.randint(1,2)
if decision == 1:
money += Cho_Han (i*10,"Even")
else:
money += Cho_Han (i*10,"Odd")
print ("You have now: $", money)
"""
"""
# This is the test for picking_card ()
for i in range (1,6):
print ("Playing Picking a card...")
money += picking_card (i*10)
print ("You have now: $", money)
"""
"""
# This is the test for roulette ()
for i in range (1,6):
print ("Playing Roulette...")
decision = random.randint(1,3)
if decision == 1:
money += roulette (i*10,"Even")
elif decision == 2:
money += roulette (i*10,"Odd")
else:
# generate a random number between 1 and 36
# 0 and 00 (37) are no valid to bet
money += roulette (i*10, random.randint(1,36))
print ("You have now: $", money)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment