This is my solution to the Codecademy Pro Project “Games of Chance” using Python to generate and work with random numbers. The aim is to prduce a number of functions simulating popular games of chance, and taking in parameters to represent a “bet” and in some cases a “call”. Includes the “extra challenge” solutions.
import random money = 100 #Write your game of chance functions here #function to play a simple game of coin toss with player guessing Heads or Tails 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 #function to play cho-han, with player guessing if the sum of two dice rolled together will be Odd or Even 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 #function to play a game where the dealer and player each draw a card from a full pack. The higher card wins. 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 #function to play basic American roulette where the player can choose Odd or Even 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 #Utility function to display running total of money remaining. def print_money(): print('You now have £' + str(money) + ' remaining.') #utility function to simulate playing the games 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)