Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 2, 2020 03:25
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/cc30de27969c93c217aa1ded2eb8a1cb to your computer and use it in GitHub Desktop.
Save codecademydev/cc30de27969c93c217aa1ded2eb8a1cb to your computer and use it in GitHub Desktop.
Codecademy export
import random
money = 100
#Write your game of chance functions here
def flip_coin(bet, guess):
print("You're now playing 'flip-coin'!")
if bet > money:
print("You do not have enough money to bet!")
elif bet <= 0:
print("invalid bet!")
else:
print("Nice bet!")
result = random.randint(0, 1)
if result == 0 and guess.lower() == 'tails' or result == 1 and guess.lower() == 'heads':
print("You guess it correctly and you got ${}. Now you have ${}.".format(bet, money + bet))
return +bet
elif result == 0 and guess.lower() == 'heads' or result == 1 and guess.lower() == 'tails':
print("You guess it incorrectly and you lost ${}. Now you have ${}.".format(bet, money - bet))
return -bet
else:
print("Invalid guess! Please try again.")
def dice_game(bet, dice_guess):
print("You're now playing 'dice-game'!")
if bet > money:
print("You do not have enough money to bet!")
elif bet <= 0:
print("invalid bet!")
else:
print("Nice bet!")
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
sum_dice = dice1 + dice2
if sum_dice % 2 == 0:
dice_guesses = 'Even'
else:
dice_guesses = 'Odd'
if (dice_guess == 'Even' or dice_guess == 'Odd') and dice_guess == dice_guesses:
print("Your total dice score is {} and you won ${}. Now you have ${}.".format(sum_dice, bet, money + bet))
return +bet
elif (dice_guess == 'Even' or dice_guess == 'Odd') and dice_guess != dice_guesses:
print("Your total dice score is {} and you lost ${}. Now you have ${}.".format(sum_dice, bet, money - bet))
return -bet
else:
print("Invalid dice_guess. Please try again!")
def play_cards(bet):
print("You're now playing 'card-game'!")
if bet > money:
print("You do not have enough money to bet!")
elif bet <= 0:
print("invalid bet!")
else:
print("Nice bet!")
card1 = random.randint(1, 10)
card2 = random.randint(1, 10)
if card1 > card2:
print("Your card is higher than your oppponent's so you won ${}. Now you have ${}.".format(bet, money + bet))
return +bet
elif card1 < card2:
print("Your card is lower than your oppponent's so you lost ${}. Now you have ${}.".format(bet, money - bet))
return -bet
else:
print("It's a tie! Your money is ${}".format(money))
def roulette(bet, guess):
print("You're now playing 'roulette'!")
if bet > money:
print("You do not have enough money to bet!")
elif bet <= 0:
print("invalid bet!")
else:
print("Nice bet!")
ball = random.randint(0, 36)
result = random.randint(0, 37)
if result == 37:
print("You landed on 00")
else:
print("You got another chance to bet!")
if guess == ball:
print("You guessed it! You won ${} and your money is now ${}.".format(bet, bet + money))
return +bet
elif guess != ball:
print("You didn't guess it! You lost ${} and your money is now ${}.".format(bet, money - bet))
return -bet
elif guess.lower() == 'even' and ball % 2 == 0 or guess.lower() == 'odd' and ball % 2 != 0:
print("You guessed it! You won ${} and your money is now ${}.".format(bet, bet + money))
return+ bet
elif guess.lower() == 'even' and ball % 2 != 0 or guess.lower() == 'odd' and ball % 2 == 0:
print("You didn't guess it! You lost ${} and your money is now ${}.".format(bet, money - bet))
return -bet
else:
print("Invalid guess")
#Call your game of chance functions here
money += flip_coin(23, 'Heads')
money += dice_game(37, 'Even')
money += play_cards(67)
money += roulette(45, 'even')
money += roulette(12, 34)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment