Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 5, 2020 19:23
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/13883dfc5f2e522d12349c89640baaf8 to your computer and use it in GitHub Desktop.
Save codecademydev/13883dfc5f2e522d12349c89640baaf8 to your computer and use it in GitHub Desktop.
Codecademy export
import random
###############################################
# Coin Flip Game
money = 100
def coin_flip(heads_or_tails,bet):
# Variables below aren't needed, but are for clarification
heads = 1
tails = 2
# assign random flip to the flip variable
flip = random.randint(1,2)
# Bet can't be negative or more than your balance
if bet < 0:
print("Your bet must be greater than zero. Please try again.\n")
elif bet > money:
print("You don't have that much money. Please try again.\n")
# Run game and print outcome and new balance
else:
if heads_or_tails.lower() == "heads" and flip == 1:
print("That's correct - it was heads!")
print("You won " + str(bet) + " dollars!\n")
return bet
elif heads_or_tails.lower() == "heads" and flip == 2:
print("That's not correct - it was tails!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
elif heads_or_tails.lower() == "tails" and flip == 2:
print("That's correct - it was tails!")
print("You won " + str(bet) + " dollars!.\n")
return bet
elif heads_or_tails.lower() == "tails" and flip == 1:
print("That's not correct - it was heads!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
###############################################
# Cho-Han Game
money = 100
def roll_dice(guess, bet):
# Store random rolls, total and even/odd in variables
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
total_roll = dice1 + dice2
even_odd = dice1 % dice2
# Bet can't be negative or more than your balance
if bet < 0:
print("Your bet must be greater than zero. Please try again.\n")
elif bet > money:
print("You don't have that much money. Please try again.\n")
else:
# If even was guessed correctly...
if guess.lower() == "even" and even_odd == 0:
print("You rolled a(n) " + str(dice1) + " and a(n) " + str(dice2) + " for a total of " + str(total_roll) +".")
print("You guessed " + guess.lower() + ".")
print(str(total_roll) + " is an even number.")
print("Congrats! You won " + str(bet) + " dollars!\n")
return bet
# If even was guessed incorrectly
elif guess.lower() == "even" and even_odd != 0 :
print("You rolled a(n) " + str(dice1) + " and a(n) " + str(dice2) + " for a total of " + str(total_roll) +".")
print("You guessed " + guess.lower() + ".")
print(str(total_roll) + " is an odd number.")
print("You lost " + str(bet) + " dollars.\n")
print("Better luck next time!\n")
return -bet
# If odd was guessed correctly...
if guess.lower() == "odd" and even_odd != 0:
print("You rolled a(n) " + str(dice1) + " and a(n) " + str(dice2) + " for a total of " + str(total_roll) +".")
print("You guessed " + guess.lower() + ".")
print(str(total_roll) + " is an odd number.")
print("Congrats! You won " + str(bet) + " dollars!\n")
return bet
# If odd was guessed incorrectly
elif guess.lower() == "odd" and even_odd == 0 :
print("You rolled a(n) " + str(dice1) + " and a(n) " + str(dice2) + " for a total of " + str(total_roll) +".")
print("You guessed " + guess.lower() + ".")
print(str(total_roll) + " is an even number.")
print("You lost " + str(bet) + " dollars.")
print("Better luck next time!\n")
return -bet
###############################################
# Card game
money = 100
def card_game(bet):
deck = [2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,
9,10,10,10,10,"Jack","Jack","Jack","Jack","Queen","Queen","Queen",
"Queen","King","King","King","King","Ace","Ace","Ace","Ace"]
# Bet can't be negative or more than your balance
if bet < 0:
print("Your bet must be greater than zero. Please try again.\n")
elif bet > money:
print("You don't have that much money. Please try again.\n")
else:
# First draw occurs, if face card, convert to numeric value
draw1 = random.choice(deck)
if draw1 == "Jack":
draw1_value = 11
elif draw1 == "Queen":
draw1_value = 12
elif draw1 == "King":
draw1_value = 13
elif draw1 == "Ace":
draw1_value = 14
else:
draw1_value = draw1
# Remove that card from the deck
deck.remove(draw1)
# Second draw occurs, if face card, convert to numeric value
draw2 = random.choice(deck)
if draw2 == "Jack":
draw2_value = 11
elif draw2 == "Queen":
draw2_value = 12
elif draw2 == "King":
draw2_value = 13
elif draw2 == "Ace":
draw2_value = 14
else:
draw2_value = draw2
# We'll assume you are the second draw
# Determine winner
# You win
if draw1_value < draw2_value:
print("Your opponent drew a(n) " + str(draw1) + " and you drew a(n) " + str(draw2) + ".")
print("Congrats!")
print("You won " + str(bet) + " dollars.\n")
return bet
#You lose
elif draw1_value > draw2_value:
print("Your opponent drew a(n) " + str(draw1) + " and you drew a(n) " + str(draw2) + ".")
print("Better luck next time!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
#You tie
elif draw1_value == draw2_value:
print("Your opponent drew a(n) " + str(draw1) + " and you drew a(n) " + str(draw2) + ".")
print("It's a tie!")
print("You didn't lose any money!\n")
return 0
###############################################
# Roulette game
money = 100
def roulette_game(guess, bet):
# Bet can't be negative or more than your balance
if bet < 0:
print("Your bet must be greater than zero. Please try again.\n")
elif bet > money:
print("You don't have that much money. Please try again.\n")
else:
# Start game and store result of spin
spin = random.randint(0,37)
if spin == 37:
spin = "00"
elif spin == 0:
spin = 0
elif spin % 2 ==0:
even_odd = "even"
elif spin % 2 != 0:
even_odd = "odd"
# Check even/odd results after concluding guess was a string
if isinstance(guess,str):
if guess.lower() == "even" and even_odd == "even":
print("The roll was a(n) " + str(spin) + " which is even.")
print("You won " + str(bet) + " dollars!\n")
return bet
elif guess.lower() == "even" and even_odd == "odd":
print("The roll was a(n) " + str(spin) + " which is odd.")
print("Better luck next time!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
elif guess.lower() == "odd" and even_odd == "odd":
print("The roll was a(n) " + str(spin) + " which is odd.")
print("You won " + str(bet) + " dollars!\n")
return bet
elif guess.lower() == "odd" and even_odd == "even":
print("The roll was a(n) " + str(spin) + " which is even.")
print("Better luck next time!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
elif guess == "00" and spin == "00":
print("The roll was a 00!!!")
double_zero_payout = bet*35
print("You won " + str(double_zero_payout) + " dollars!\n")
return double_zero_payout
else:
print("You guessed a(n) " + str(guess) + " would be rolled and a(n) " + str(spin) + " was rolled")
print("Better luck next time!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
# Check number results
elif guess == 0 and spin == 0:
print("The roll was a 0!!!")
zero_payout = bet*35
print("You won " + str(zero_payout) + " dollars!\n")
return zero_payout
elif guess == spin:
print("You guessed a(n) " + str(guess) + " would be rolled and a(n) " + str(spin) + " was rolled")
straight_up_payout = bet*35
print("You win " + str(straight_up_payout) + " dollars!")
return straight_up_payout
else:
print("You guessed a(n) " + str(guess) + " would be rolled and a(n) " + str(spin) + " was rolled")
print("Better luck next time!")
print("You lost " + str(bet) + " dollars.\n")
return -bet
# Total up all games and subtract from beginning money
game_1_total = coin_flip("tails",10)
game2_total = roll_dice("odd",50)
game3_total = card_game(30)
game4_total = roulette_game("00",10)
ending_balance = money + game_1_total + game2_total + game3_total + game4_total
print("Your total balance is now " + str(ending_balance) + ".\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment