piotrirving

Games of Chance Python Project

May 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. https://www.codecademy.com/practice/projects/games-of-chance
  2.  
  3. import random
  4. #Games - Coin Flip, Cho-Han, Highest Card, Roulette
  5. money = 100
  6.  
  7. #Write your game of chance functions here
  8. #Coin Flip
  9. def coin_flip(guess, bet):
  10.   global money
  11.   if guess == "Heads" or guess == "Tails":
  12.     if bet <= money:
  13.       side = random.randint(0, 1)
  14.       #Convert number to Heads/Tails
  15.       #Heads is 0, Tails = 1
  16.       if side == 0:
  17.         side = "Heads"
  18.       else:
  19.         side = "Tails"
  20.       # check if winner
  21.       winner = ""
  22.       if guess == side:
  23.         money += bet
  24.         winner = "You won"
  25.       else:
  26.         money -= bet
  27.         winner = "You lost"
  28.       print(winner + "! You guessed " + guess+ ", the answer was " + side +", you now have $" + str(money)+ ".")
  29.  
  30.     else:
  31.       print("Invalid Bet")
  32.   else:
  33.     print("Invalid Guess")
  34.  
  35.  
  36.  
  37. #Cho-Han
  38. def cho_han(guess, bet):
  39.   global money
  40.   if guess == "Odd" or guess == "Even":
  41.     if bet <= money:
  42.       #rolling die
  43.       die1 = random.randint(1, 6)
  44.       die2 = random.randint(1, 6)
  45.       total_die = die1 + die2
  46.       #check answers
  47.       winner = ""
  48.       if total_die % 2 == 0 and guess == "Even" or total_die % 2 != 0 and guess == "Odd":
  49.         money += bet
  50.         winner = "You won"
  51.       else:
  52.         money -= bet
  53.         winner = "You lost"
  54.       print(winner + "! You guessed " + guess + ", the die rolled: " + str(total_die) + " ("+ str(die1) + " & " + str(die2) + ")" + " , you now have $" + str(money)+ ".")
  55.     else:
  56.       print("Invalid Bet")
  57.   else:
  58.     print("Invalid Guess")
  59.  
  60. #Card Picker
  61. def card_name(value):
  62.   if 1 <= value <= 10:
  63.     return value
  64.   elif value == 11:
  65.     return "Jack"
  66.   elif value == 12:
  67.     return "Queen"
  68.   elif value == 13:
  69.     return "King"
  70.   elif value == 14:
  71.     return "Ace"
  72.   else:
  73.     return "error in card value"
  74.  
  75. def card_picker(bet):
  76.   global money
  77.   deck_of_cards = []
  78.   #create deck of cards
  79.   for value in range(1, 15):
  80.     for multiples in range(4):
  81.       deck_of_cards.append(value)
  82.   #choose cards    
  83.   player1 = deck_of_cards.pop(random.randint(0, len(deck_of_cards)-1))
  84.   player2 = deck_of_cards.pop(random.randint(0, len(deck_of_cards)-1))
  85.   card1 = card_name(player1)
  86.   card2 = card_name(player2)
  87.   if player1 == player2:
  88.     print("There was a tie! Both you and the dealer chose {card}. You now have ${money}".format(card=card1, money=money))
  89.   elif player1 > player2:
  90.     money += bet
  91.     print("You won! You chose {card1}, the dealer chose {card2}. You now have ${money}".format(card1=card1, card2=card2, money=money))
  92.   else:
  93.     money -= bet
  94.     print("You lost! You chose {card1}, the dealer chose {card2}. You now have ${money}".format(card1=card1, card2=card2, money=money))
  95.  
  96. #Roulette
  97. double_zero_wheel  = ["0", "28", "9", "26", "30", "11", "7", "20", "32", "17", "5", "22", "34", "15", "3", "24", "36", "13", "1", "00", "27", "10", "25", "29", "12", "8", "19", "31", "18", "6", "21", "33", "16", "4", "23", "35", "14", "2"]
  98. available_bets = ["0", "00", "Odd", "Even"]
  99.  
  100. def roulette(bet_name, bet):
  101.   global money
  102.   global available_bets
  103.   winner = ""
  104.   #rolls
  105.   roll = random.randint(0,len(double_zero_wheel)-1)
  106.   roll_value = double_zero_wheel[roll]
  107.   #input checks
  108.   if bet_name in available_bets:
  109.     if bet <= money:
  110.       #bet processing
  111.       #bet 0
  112.       if bet_name == "0":
  113.         if roll_value == "0":
  114.           money += bet * 35
  115.           winner = "You won!"
  116.         else:
  117.           money -= bet
  118.           winner = "You lost!"
  119.       #bet 00
  120.       elif bet_name == "00":
  121.         if roll_value == "00":
  122.           money += bet * 35
  123.           winner = "You won!"
  124.         else:
  125.           money -= bet
  126.           winner = "You lost!"
  127.       #bet Odd
  128.       elif bet_name == "Odd":
  129.         if not(roll_value == "0" or roll_value == "00") and int(roll_value) % 2 != 0:
  130.           money += bet
  131.           winner = "You won!"
  132.         else:
  133.           money -= bet
  134.           winner = "You lost!"
  135.       elif bet_name == "Even":
  136.         if not(roll_value == "0" or roll_value == "00") and int(roll_value) % 2 == 0:
  137.           money += bet
  138.           winner = "You won!"
  139.         else:
  140.           money -= bet
  141.           winner = "You lost!"
  142.       print("{winner} You chose to bet {bet_name} and the roll was {roll_value}. You now have ${money}".format(winner=winner,bet_name=bet_name, roll_value=roll_value, money=money))
  143.     else:
  144.        print("Invalid Bet")  
  145.   else:
  146.     print("bet type not available")
  147.  
  148.  
  149.  
  150.  
  151. #Call your game of chance functions here
  152. coin_flip("Tails", 10)
  153. cho_han("Even", 10)
  154. card_picker(5)
  155. roulette("00", 10)
Add Comment
Please, Sign In to add comment