daily pastebin goal
45%
SHARE
TWEET

Activity3

oanap123 Jul 11th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. def main():
  4.     #Initialize variables
  5.     play_again_counter = 0
  6.     winnings = 0
  7.     bet = 25
  8.     money = 1000
  9.     name = ""
  10.  
  11.     restore()
  12.  
  13.     if name != "" and money != -1:
  14.         resume = input("Resume saved game '" + name + "'? (y/n) ")
  15.         if resume == 'y':
  16.             print(name + "has $" + money)
  17.             print()
  18.  
  19.             #Run game and loop if the user wants to keep playing
  20.             while play_again_counter == 0:
  21.                 bet = input_bet(bet, money)
  22.                 if bet == 0:
  23.                     print("Player has quit")
  24.                     save(name, money)
  25.                     exit()
  26.                 else:
  27.                     play_again_counter = 0
  28.                 winnings = play_hand(name, bet)
  29.                 money += winnings
  30.                 print()
  31.                 print(name + " has $" + str(money))
  32.                 print()
  33.  
  34.         else:
  35.             #Get user's name and print out starting value
  36.             name = input("Name? ")
  37.             print(name + " has $1,000")
  38.             print()
  39.  
  40.             #Run game and loop if the user wants to keep playing
  41.             while play_again_counter == 0:
  42.                 bet = input_bet(bet, money)
  43.                 if bet == 0:
  44.                     print("Player has quit")
  45.                     save(name, money)
  46.                     exit()
  47.                 else:
  48.                     play_again_counter = 0
  49.                 winnings = play_hand(name, bet)
  50.                 money += winnings
  51.                 print()
  52.                 print(name + " has $" + str(money))
  53.                 print()
  54.  
  55.     else:
  56.         #Get user's name and print out starting value
  57.         name = input("Name? ")
  58.         print(name + " has $1,000")
  59.         print()
  60.  
  61.         #Run game and loop if the user wants to keep playing
  62.         while play_again_counter == 0:
  63.             bet = input_bet(bet, money)
  64.             if bet == 0:
  65.                 print("Player has quit")
  66.                 save(name, money)
  67.                 exit()
  68.             else:
  69.                 play_again_counter = 0
  70.             winnings = play_hand(name, bet)
  71.             money += winnings
  72.             print()
  73.             print(name + " has $" + str(money))
  74.             print()
  75.  
  76.  
  77. def save(name, money):
  78.     save_info = open('blackjack.save', 'w')
  79.  
  80.     save_info.write(str(name) + '\n')
  81.     save_info.write(str(money) + '\n')
  82.  
  83.     save_info.close()
  84.  
  85.  
  86. def restore():
  87.     try:
  88.         read_info = open('blackjack.save', 'r')
  89.  
  90.         #Read the lines of text
  91.         name_in_file = read_info.readline()
  92.         money_in_file = read_info.readline()
  93.  
  94.         #Strip \n from the text lines
  95.         name = name_in_file.rstrip('\n')
  96.         money = money_in_file.rstrip('\n')
  97.  
  98.     except FileNotFoundError:
  99.         name = ""
  100.         money = -1
  101.  
  102.     return name, money
  103.  
  104.  
  105. def input_bet(bet, money):
  106.     while True:
  107.         #Ask for bet
  108.         bet_check = input("Bet? (0 to quit, Enter to stay at $" + str(bet) + ") ")
  109.  
  110.         #Check if player hit Enter
  111.         if bet_check == "":
  112.             #Check whether or not player has enough money in account to continue
  113.             #with the same bet
  114.             if bet < money:
  115.                 break
  116.             elif bet > money:
  117.                 print("You cannot bet more than $" + money)
  118.  
  119.         else:
  120.             #Check if input was integer
  121.             try:
  122.                 bet_check = int(bet_check)
  123.                 #Check if they want to quit
  124.                 if bet_check == 0:
  125.                     return 0
  126.                 #Check that bet check is not negative
  127.                 elif bet_check < 0:
  128.                     print("You cannot bet a negative amount")
  129.                 #Check that bet is less than amount in account
  130.                 else:
  131.                     if bet_check > money:
  132.                         print("You cannot bet more than $" + str(money))
  133.                     else:
  134.                         bet = bet_check
  135.                         break
  136.             #If input was not an integer
  137.             except ValueError:
  138.                 #Output error if input was a float
  139.                 try:
  140.                     bet_check = float(bet_check)
  141.                     print("You must bet a whole number")
  142.                 #Output error if input was not a number
  143.                 except ValueError:
  144.                     print("You must type a number")
  145.  
  146.     return bet
  147.  
  148.  
  149. def play_hand(name, bet):
  150.     #Deal intial cards for dealer and player
  151.     dealer_num = random.randint(2, 11)
  152.     print("Dealer received card of value " + str(dealer_num))
  153.     player_num1 = random.randint(2, 11)
  154.     print(name + " received card of value " + str(player_num1))
  155.     player_num2 = random.randint(2, 11)
  156.     print(name + " received card of value " + str(player_num2))
  157.  
  158.     #Calculate totals for dealer and player
  159.     dealer_total = dealer_num
  160.     print("Dealer total: " + str(dealer_total))
  161.     player_total = player_num1 + player_num2
  162.     print(name + " total: " + str(player_total))
  163.  
  164.     #Initialize counter variables
  165.     dealer_counter = 0
  166.     player_counter = 0
  167.  
  168.     #Check if player total is 21
  169.     if player_total == 21:
  170.         #Continue picking cards for dealer if player gets 21
  171.         while dealer_counter == 0:
  172.             dealer_card = random.randint(2, 11)
  173.             dealer_total += dealer_card
  174.             print("Dealer received card of value " + str(dealer_card))
  175.             print("Dealer total: " + str(dealer_total))
  176.             print(name + " total: " + str(player_total))
  177.             print()
  178.             if dealer_total >= 17:
  179.                 dealer_counter = 1
  180.         #Output results for dealer ending less than 21
  181.         if dealer_total < 21:
  182.             print(name + " wins")
  183.             winnings = bet
  184.             return winnings
  185.         #Output results for dealer tie with player
  186.         elif dealer_total == 21:
  187.             print("Push")
  188.             winnings = 0
  189.             return winnings
  190.         #Output results for dealer bust
  191.         else:
  192.             print("Dealer bust")
  193.             winnings = bet
  194.             return winnings
  195.  
  196.     #Check if player total is less than 21
  197.     elif player_total < 21:
  198.         #Ask player what move they want to make
  199.         while player_counter == 0:
  200.             move = input("Move? (h/s) ")
  201.             print()
  202.             #Continue dealing cards to player and outputting results for hit
  203.             if move == 'h':
  204.                 player_card = random.randint(2, 11)
  205.                 player_total += player_card
  206.                 print(name + " received card of value " + str(player_card))
  207.                 print("Dealer total: " + str(dealer_total))
  208.                 print(name + " total: " + str(player_total))
  209.                 print()
  210.                 if player_total >= 21:
  211.                     player_counter = 1
  212.             #Stop dealing cards to player for stay
  213.             elif move == 's':
  214.                 player_counter = 1
  215.  
  216.         #Check if player does not bust
  217.         if player_total <= 21:
  218.             #Continue picking cards for dealer if player stays
  219.             while dealer_counter == 0:
  220.                 dealer_card = random.randint(2, 11)
  221.                 dealer_total += dealer_card
  222.                 print("Dealer received card of value " + str(dealer_card))
  223.                 print("Dealer total: " + str(dealer_total))
  224.                 print(name + " total: " + str(player_total))
  225.                 print()
  226.  
  227.                 #Pick cards for dealer while his total is less than 17
  228.                 if dealer_total >= 17:
  229.                     dealer_counter = 1
  230.  
  231.             #Check if dealer ends less than 21
  232.             if dealer_total < 21:
  233.                 #Compare dealer total to player total and determine winner
  234.                 if dealer_total < player_total:
  235.                     print(name + " wins")
  236.                     winnings = bet
  237.                     return winnings
  238.                 elif dealer_total > player_total:
  239.                     print("Dealer wins")
  240.                     winnings = -bet
  241.                     return winnings
  242.                 else:
  243.                     print("Push")
  244.                     winnings = 0
  245.                     return winnings
  246.             #Check if dealer ends at 21
  247.             elif dealer_total == 21:
  248.                 #Compare dealer total to player total and determine winner
  249.                 if dealer_total == player_total:
  250.                     print("Push")
  251.                     winnings = 0
  252.                     return winnings
  253.                 else:
  254.                     print("Dealer wins")
  255.                     winnings = -bet
  256.                     return winnings
  257.             #Check if dealer busts
  258.             else:
  259.                 print("Dealer bust")
  260.                 winnings = bet
  261.                 return winnings
  262.  
  263.         #Check if player busts after move
  264.         elif player_total > 21:
  265.             print(name + " bust")
  266.             winnings = -bet
  267.             return winnings
  268.  
  269.     #Check if player busts after initial pick
  270.     else:
  271.         print(name + " bust")
  272.         winnings = -bet
  273.         return winnings
  274.  
  275. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top