Advertisement
oanap123

Python Project 2

Jul 4th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. import random
  2.  
  3. def main():
  4.     #Initialize variables
  5.     play_again_counter = 0
  6.     winnings = 0
  7.     money = 1000
  8.  
  9.     #Get user's name and print out starting value
  10.     name = input("Name? ")
  11.     print(name + " has $1,000")
  12.     print()
  13.  
  14.     #Output updated account total for player
  15.     while play_again_counter == 0:
  16.         winnings = play_hand(name)
  17.         money += winnings
  18.         print()
  19.         print(name + " has $" + str(money))
  20.         print()
  21.  
  22.         #Ask player if they want to play again
  23.         check = input("Play again? (y/n) ")
  24.         print()
  25.         if check == 'y':
  26.             play_again_counter = 0
  27.         else:
  28.             play_again_counter = 1
  29.  
  30. def input_bet(bet, money):
  31.     #Ask for bet
  32.     bet = input("Bet? (0 to quit, Enter to stay at $25) ")
  33.  
  34.     #Check that bet is a whole number
  35.     try:
  36.         bet % 1 == 0
  37.     except TypeError:
  38.         print("You must bet a whole number")
  39.  
  40.     #Check that bet is less than amount in account
  41.     try:
  42.         bet <= money
  43.     except ValueError:
  44.         print("You cannot bet more than " + money)
  45.  
  46.     #Check that bet is not negative
  47.     try:
  48.         bet >= 0
  49.     except ValueError:
  50.         print("You cannot bet a negative amount")
  51.  
  52.     #Quit game if player inputs 0
  53.     if bet == 0:
  54.         print("Player has quit")
  55.         break
  56.     #Check if they have enough money in the account to bet the same amount
  57.     elif bet == "":
  58.         if bet < money:
  59.             bet = bet
  60.         elif bet > money:
  61.             print("You cannot bet more than " + money)
  62.  
  63.  
  64. def play_hand(name):
  65.     #Deal intial cards for dealer and player
  66.     dealer_num = random.randint(2, 11)
  67.     print("Dealer received card of value " + str(dealer_num))
  68.     player_num1 = random.randint(2, 11)
  69.     print(name + " received card of value " + str(player_num1))
  70.     player_num2 = random.randint(2, 11)
  71.     print(name + " received card of value " + str(player_num2))
  72.  
  73.     #Calculate totals for dealer and player
  74.     dealer_total = dealer_num
  75.     print("Dealer total: " + str(dealer_total))
  76.     player_total = player_num1 + player_num2
  77.     print(name + " total: " + str(player_total))
  78.  
  79.     #Initialize counter variables
  80.     dealer_counter = 0
  81.     player_counter = 0
  82.  
  83.     #Check if player total is 21
  84.     if player_total == 21:
  85.         #Continue picking cards for dealer if player gets 21
  86.         while dealer_counter == 0:
  87.             dealer_card = random.randint(2, 11)
  88.             dealer_total += dealer_card
  89.             print("Dealer received card of value " + str(dealer_card))
  90.             print("Dealer total: " + str(dealer_total))
  91.             print(name + " total: " + str(player_total))
  92.             print()
  93.             if dealer_total >= 17:
  94.                 dealer_counter = 1
  95.         #Output results for dealer ending less than 21
  96.         if dealer_total < 21:
  97.             print(name + " wins")
  98.             winnings = 25
  99.             return winnings
  100.         #Output results for dealer tie with player
  101.         elif dealer_total == 21:
  102.             print("Push")
  103.             winnings = 0
  104.             return winnings
  105.         #Output results for dealer bust
  106.         else:
  107.             print("Dealer bust")
  108.             winnings = 25
  109.             return winnings
  110.  
  111.     #Check if player total is less than 21
  112.     elif player_total < 21:
  113.         #Ask player what move they want to make
  114.         while player_counter == 0:
  115.             move = input("Move? (h/s) ")
  116.             print()
  117.             #Continue dealing cards to player and outputting results for hit
  118.             if move == 'h':
  119.                 player_card = random.randint(2, 11)
  120.                 player_total += player_card
  121.                 print(name + " received card of value " + str(player_card))
  122.                 print("Dealer total: " + str(dealer_total))
  123.                 print(name + " total: " + str(player_total))
  124.                 print()
  125.                 if player_total >= 21:
  126.                     player_counter = 1
  127.             #Stop dealing cards to player for stay
  128.             elif move == 's':
  129.                 player_counter = 1
  130.  
  131.         #Check if player does not bust
  132.         if player_total <= 21:
  133.             #Continue picking cards for dealer if player stays
  134.             while dealer_counter == 0:
  135.                 dealer_card = random.randint(2, 11)
  136.                 dealer_total += dealer_card
  137.                 print("Dealer received card of value " + str(dealer_card))
  138.                 print("Dealer total: " + str(dealer_total))
  139.                 print(name + " total: " + str(player_total))
  140.                 print()
  141.  
  142.                 #Pick cards for dealer until his total is less than 17
  143.                 if dealer_total >= 17:
  144.                     dealer_counter = 1
  145.                 if dealer_total > player_total:
  146.                     dealer_counter = 1
  147.             #Check if dealer ends less than 21
  148.             if dealer_total < 21:
  149.                 #Compare dealer total to player total and determine winner
  150.                 if dealer_total < player_total:
  151.                     print(name + " wins")
  152.                     winnings = 25
  153.                     return winnings
  154.                 elif dealer_total > player_total:
  155.                     print("Dealer wins")
  156.                     winnings = -25
  157.                     return winnings
  158.                 else:
  159.                     print("Push")
  160.                     winnings = 0
  161.                     return winnings
  162.             #Check if dealer ends at 21
  163.             elif dealer_total == 21:
  164.                 #Compare dealer total to player total and determine winner
  165.                 if dealer_total == player_total:
  166.                     print("Push")
  167.                     winnings = 0
  168.                     return winnings
  169.                 else:
  170.                     print("Dealer wins")
  171.                     winnings = -25
  172.                     return winnings
  173.             #Check if dealer busts
  174.             else:
  175.                 print("Dealer bust")
  176.                 winnings = 25
  177.                 return winnings
  178.  
  179.         #Check if player busts after move
  180.         elif player_total > 21:
  181.             print(name + " bust")
  182.             winnings = -25
  183.             return winnings
  184.  
  185.     #Check if player busts after initial pick
  186.     else:
  187.         print(name + " bust")
  188.         winnings = -25
  189.         return winnings
  190.  
  191. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement