Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
card_values = { | |
2: "two", | |
3: "three", | |
4: "four", | |
5: "five", | |
6: "six", | |
7: "seven", | |
8: "eight", | |
9: "nine", | |
10: "ten", | |
11: "jack", | |
12: "queen", | |
13: "king", | |
14: "ace" | |
} | |
money = 100 | |
#Write your game of chance functions here | |
#Coin toss: | |
def coin_flip1(call,bet): | |
print("The coin toss is starting!") | |
print(f"You have placed a ${bet} bet on '{call}'.") | |
stash = money | |
num = random.randint(1,2) | |
if bet > stash: | |
print("You don't have enough money for that bet.") | |
return stash | |
if num == 1: | |
side = "heads" | |
else: | |
side = "tails" | |
print(f"Coinflip one results in {side}.") | |
if side == call: | |
stash += bet | |
print("You've won the toss!") | |
print(f"You win ${bet}.") | |
print(f"You now have ${stash}!") | |
else: | |
stash -= bet | |
print("You've lost the toss. :(") | |
print(f"You lose ${bet}.") | |
print(f"You now have ${stash}.") | |
return stash | |
#Cho-han: | |
def cho_han(prediction,bet): | |
print("Your game of Cho Han is starting!") | |
print(f"You have placed a ${bet} bet on '{prediction}'.") | |
stash = money | |
if bet > stash: | |
print("You don't have enough money for that bet.") | |
return stash | |
dice1 = random.randint(1,6) | |
dice2 = random.randint(1,6) | |
num = dice1 + dice2 | |
print (f"Your Cho Han dice roll is {num}.") | |
if (num % 2) == 0: | |
result = "even" | |
else: | |
result = "odd" | |
if result == prediction: | |
stash += bet | |
print("You've won at Cho Han!") | |
print(f"You win ${bet}.") | |
print(f"You now have ${stash}!") | |
else: | |
stash -= bet | |
print("You've lost at Cho Han. :(") | |
print(f"You lose ${bet}.") | |
print(f"You now have ${stash}.") | |
return stash | |
#Card war game: | |
def card_war(bet): | |
print("Your card war is starting!") | |
stash = money | |
if bet > stash: | |
print("You don't have enough money for that bet.") | |
return stash | |
draw1 = random.choice(list(card_values.keys())) | |
draw2 = random.choice(list(card_values.keys())) | |
if draw1 == 8 or draw1 == 14: | |
print(f"You have drawn an '{card_values[draw1]}' card.") | |
else: | |
print(f"You have drawn a '{card_values[draw1]}' card.") | |
if draw2 == 8 or draw2 == 14: | |
print(f"Your opponent has drawn an '{card_values[draw2]}' card.") | |
else: | |
print(f"Your opponent has drawn a '{card_values[draw2]}' card.") | |
if draw1 > draw2: | |
stash += bet | |
print("You've won the card draw!") | |
print(f"You win ${bet}.") | |
print(f"You now have ${stash}!") | |
elif draw2 > draw1: | |
stash -= bet | |
print("You've lost the card draw!") | |
print(f"You lose ${bet}.") | |
print(f"You now have ${stash}.") | |
else: | |
print("It's a tie!") | |
return stash | |
#Roulette: | |
def roulette(selection, bet): | |
print("Your roulette game is starting!") | |
print("Enter a number between 1 and 36, or enter any of the following options:") | |
print("Even") | |
print("Odd") | |
print("Lows") | |
print("Highs") | |
print("First Dozen") | |
print("Second Dozen") | |
print("Third Dozen") | |
print() | |
print(f"You have placed a ${bet} bet on '{selection}'.") | |
stash = money | |
win = " " | |
if bet > stash: | |
print("You don't have enough money for that bet.") | |
return stash | |
if bet <= 0: | |
print("You can't bet a negative amount.") | |
return stash | |
num = random.randint(0, 36) | |
full = np.arange(1, 36, 1) | |
even = np.arange(2, 37, 2).tolist() | |
odd = np.arange(1, 36, 2).tolist() | |
lows = np.arange(1, 19, 1).tolist() | |
highs = np.arange(19, 37, 1).tolist() | |
first_dozen = np.arange(1, 13, 1).tolist() | |
second_dozen = np.arange(13, 25, 1).tolist() | |
third_dozen = np.arange(25, 37, 1).tolist() | |
if (num == 37): | |
print(f"A 00 was rolled.") | |
if (num == 8) or (num == 18): | |
print(f"An {num} was rolled.") | |
else: | |
print(f"A {num} was rolled.") | |
if num == 0 or num == 37: | |
stash -= bet | |
win = False | |
elif selection in full: | |
if selection == num: | |
win = True | |
stash += (bet * 35) | |
print("You've won at roulette!") | |
print(f"You win ${bet * 35}.") | |
else: | |
win = False | |
elif selection == "Evens": | |
if num in even: | |
win = True | |
stash += bet | |
print("You've won at roulette!") | |
print(f"You win ${bet}.") | |
else: | |
win = False | |
elif selection == "Odds": | |
if num in odd: | |
win = True | |
stash += bet | |
print("You've won at roulette!") | |
print(f"You win ${bet}.") | |
else: | |
win = False | |
elif selection == "Highs": | |
if num in lows: | |
win = True | |
stash += bet | |
print("You've won at roulette!") | |
print(f"You win ${bet}.") | |
else: | |
win = False | |
elif selection == "Lows": | |
if num in highs: | |
win = True | |
stash += bet | |
print("You've won at roulette!") | |
print(f"You win ${bet}.") | |
else: | |
win = False | |
elif selection == "First Dozen": | |
if num in first_dozen: | |
win = True | |
stash += (bet * 2) | |
print("You've won at roulette!") | |
print(f"You win ${bet * 2}.") | |
else: | |
win = False | |
elif selection == "Second Dozen": | |
if num in second_dozen: | |
win = True | |
stash += (bet * 2) | |
print("You've won at roulette!") | |
print(f"You win ${bet * 2}.") | |
else: | |
win = False | |
elif selection == "Third Dozen": | |
if num in third_dozen: | |
win = True | |
stash += (bet * 2) | |
print("You've won at roulette!") | |
print(f"You win ${bet * 2}.") | |
else: | |
stash -= bet | |
win = False | |
if win == True: | |
print(f"You now have ${stash}.") | |
else: | |
stash -= bet | |
print("You've lost at roulette!") | |
print(f"You lose ${bet}.") | |
print(f"You now have ${stash}.") | |
return stash | |
#Call game functions. | |
print(f"You begin gaming with ${money}.") | |
print() | |
money = coin_flip1("tails",10) | |
print("") | |
money = cho_han("odd",15) | |
print("") | |
money = card_war(20) | |
print("") | |
money = roulette("First Dozen", 20) | |
print() | |
print(f"Your final stash is ${money}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment