Created
May 18, 2020 11:34
-
-
Save MrN00b0t/84a2b8ee167dd1733505127a5d89671f to your computer and use it in GitHub Desktop.
Codecademy: Games of Chance Python Project
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 | |
money = 100 | |
#Write your game of chance functions here | |
def coin_toss(guess, bet): | |
if bet > money: | |
print('You don\'t have enough green to place that bet!') | |
print('The maximum bet you can make is £' + str(money)) | |
return 0 | |
if (guess != 'Heads') and (guess != 'Tails'): | |
print('Invalid guess. You can choose Heads or Tails.') | |
return 0 | |
toss = random.randint(1, 2) | |
if (guess == 'Heads') and (toss == 1): | |
print('Lucky guess! The toss is Heads. You win!') | |
return bet | |
elif (guess == 'Tails') and (toss == 2): | |
print('Lucky guess! The toss is Tails. You win!') | |
return bet | |
else: | |
if toss == 1: | |
print('The toss was Heads. You lose.') | |
if toss == 2: | |
print('The toss was Tails. You lose.') | |
return -bet | |
def cho_han(guess, bet): | |
if bet > money: | |
print('You don\'t have enough green to place that bet!') | |
print('The maximum bet you can make is £' + str(money)) | |
return 0 | |
if (guess != 'Even') and (guess != 'Odd'): | |
print('Invalid guess. You can choose Odd or Even.') | |
return 0 | |
dice1 = random.randint(1, 6) | |
dice2 = random.randint(1, 6) | |
result = dice1 + dice2 | |
mod_result = result % 2 | |
if (guess == 'Even') and ((result % 2) == 0): | |
print('The dice rolls were ' + str(dice1) + ' and ' + str(dice2) + '. The total is ' + str(result) + ' - an EVEN number. You win!') | |
return bet | |
elif (guess == 'Odd') and ((result % 2) != 0): | |
print('The dice rolls were ' + str(dice1) + ' and ' + str(dice2) + '. The total is ' + str(result) + ' - an ODD number. You win!') | |
return bet | |
else: | |
if mod_result == 0: | |
print('The dice rolls were ' + str(dice1) + ' and ' + str(dice2) + '. The total is ' + str(result) + ' - an EVEN number.') | |
else: | |
print('The dice rolls were ' + str(dice1) + ' and ' + str(dice2) + '. The total is ' + str(result) + ' - an ODD number.') | |
print('You lose.') | |
return - bet | |
def higher_lower(bet): | |
if bet > money: | |
print('You don\'t have enough green to place that bet!') | |
print('The maximum bet you can make is £' + str(money)) | |
return 0 | |
#Create deck with four suits of value 0-12; once player 1 card is selected this value will not be available for player 2 increasing probability of a different number. | |
deck = [] | |
for i in range(13): | |
for j in range(4): | |
deck.append(i) | |
player1 = deck.pop(random.randint(0, len(deck))) | |
player2 = deck.pop(random.randint(0, len(deck))) | |
card_names = ['an Ace. Aces Low!', 'a Two.', 'a Three.', 'a Four.', 'a Five.', 'a Six.', 'a Seven.', 'an Eight.', 'a Nine.', 'a Ten.', 'a Jack.', 'a Queen.', 'a King.'] | |
print('Dealer drew ' + card_names[player1] + ' Player drew ' + card_names[player2]) | |
if player1 == player2: | |
print('We have a tie! You keep your bet.') | |
return 0 | |
elif player1 > player2: | |
print('Dealer wins! You lose your bet.') | |
return -bet | |
else: | |
print('You win! You double your bet.') | |
return bet | |
def basic_roulette(guess, bet): | |
if bet > money: | |
print('You don\'t have enough green to place that bet!') | |
print('The maximum bet you can make is £' + str(money)) | |
return 0 | |
if (guess != 'Even') and (guess != 'Odd'): | |
print('Invalid guess. You can choose Odd or Even.') | |
return 0 | |
pocket = random.randint(0, 38) | |
if pocket == 0: | |
print('The ball lands in the green 0 pocket. You lose.') | |
return -bet | |
elif pocket == 37: | |
print('The ball lands in the green 00 pocket, You lose.') | |
return - bet | |
elif (guess == 'Odd') and ((pocket % 2) != 0): | |
print('The ball lands in pocket ' + str(pocket) + ', an odd number. You win!') | |
return bet | |
elif (guess == 'Even') and ((pocket %2) == 0): | |
print('The ball lands in pocket ' + str(pocket) + ', an even number. You win!') | |
return bet | |
elif (pocket%2) == 0: | |
print('The ball lands in pocket ' + str(pocket) + ', an even number. You bet odd. You lose.') | |
return - bet | |
else: | |
print('The ball lands in pocket ' + str(pocket) + ', an odd number. You bet even. You lose.') | |
return - bet | |
def print_money(): | |
print('You now have £' + str(money) + ' remaining.') | |
def auto_game(ct_guess, ct_bet, ch_guess, ch_bet, card_bet, rou_guess, rou_bet): | |
global money | |
print('You bet £' + str(ct_bet) + ' at Coin Toss.') | |
money += coin_toss(ct_guess, ct_bet) | |
print_money() | |
print('You bet £' + str(ch_bet) + ' at Cho-Han.') | |
money += cho_han(ch_guess, ch_bet) | |
print_money() | |
print('You bet £' + str(card_bet) + ' at Hi/Lo Cards.') | |
money += higher_lower(card_bet) | |
print_money() | |
print('You bet £' + str(rou_bet) + ' at American Roulette.') | |
money += basic_roulette(rou_guess, rou_bet) | |
print('You leave the casino with £' + str(money) + ' having played all the games. Please gamble responsibly.') | |
#Call your game of chance functions here | |
auto_game('Heads', 50, 'Even', 100, 50, 'Odd', 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment