import random def main(): #Initialize variables play_again_counter = 0 winnings = 0 bet = 25 money = 1000 name = '' restore() if name != "" and money != -1: resume = input("Resume saved game '" + name + "'? (y/n) ") if resume == 'y': print(name + "has $" + money) print() #Run game and loop if the user wants to keep playing while play_again_counter == 0: bet = input_bet(bet, money) winnings = play_hand(name, bet) money += winnings print() print(name + " has $" + str(money)) print() #Loop game if player wants to play again if bet == 0: play_again_counter = 1 else: play_again_counter = 0 else: #Get user's name and print out starting value name = input("Name? ") print(name + " has $1,000") print() #Run game and loop if the user wants to keep playing while play_again_counter == 0: bet = input_bet(bet, money) winnings = play_hand(name, bet) money += winnings print() print(name + " has $" + str(money)) print() #Loop game if player wants to play again if bet == 0: play_again_counter = 1 else: play_again_counter = 0 else: #Get user's name and print out starting value name = input("Name? ") print(name + " has $1,000") print() #Run game and loop if the user wants to keep playing while play_again_counter == 0: bet = input_bet(bet, money) winnings = play_hand(name, bet) money += winnings print() print(name + " has $" + str(money)) print() #Loop game if player wants to play again if bet == 0: play_again_counter = 1 else: play_again_counter = 0 def save(name, money): save_info = open('blackjack.save', 'w') save_info.write(name + '\n') save_info.write(money + '\n') save_info.close() def restore(): try: read_info = open('blackjack.save', 'r') #Read the lines of text name_in_file = read_info.readline() money_in_file = read_info.readline() #Strip \n from the text lines name_in_file = name_in_file.rstrip('\n') money_in_file = money_in_file.rstrip('\n') except FileNotFoundError: name = "" money = -1 return name, money def input_bet(bet, money): while True: #Ask for bet bet_check = input("Bet? (0 to quit, Enter to stay at $" + str(bet) + ") ") #Check if player hit Enter if bet_check == "": #Check whether or not player has enough money in account to continue #with the same bet if bet < money: break elif bet > money: print("You cannot bet more than $" + money) else: #Check if input was integer try: bet_check = int(bet_check) #Check if they want to quit if bet_check == 0: print("Player has quit") save(name, money) bet = bet_check exit() #Check that bet check is not negative elif bet_check < 0: print("You cannot bet a negative amount") #Check that bet is less than amount in account else: if bet_check > money: print("You cannot bet more than $" + str(money)) else: bet = bet_check break #If input was not an integer except ValueError: #Output error if input was a float try: bet_check = float(bet_check) print("You must bet a whole number") #Output error if input was not a number except ValueError: print("You must type a number") return bet def play_hand(name, bet): #Deal intial cards for dealer and player dealer_num = random.randint(2, 11) print("Dealer received card of value " + str(dealer_num)) player_num1 = random.randint(2, 11) print(name + " received card of value " + str(player_num1)) player_num2 = random.randint(2, 11) print(name + " received card of value " + str(player_num2)) #Calculate totals for dealer and player dealer_total = dealer_num print("Dealer total: " + str(dealer_total)) player_total = player_num1 + player_num2 print(name + " total: " + str(player_total)) #Initialize counter variables dealer_counter = 0 player_counter = 0 #Check if player total is 21 if player_total == 21: #Continue picking cards for dealer if player gets 21 while dealer_counter == 0: dealer_card = random.randint(2, 11) dealer_total += dealer_card print("Dealer received card of value " + str(dealer_card)) print("Dealer total: " + str(dealer_total)) print(name + " total: " + str(player_total)) print() if dealer_total >= 17: dealer_counter = 1 #Output results for dealer ending less than 21 if dealer_total < 21: print(name + " wins") winnings = bet return winnings #Output results for dealer tie with player elif dealer_total == 21: print("Push") winnings = 0 return winnings #Output results for dealer bust else: print("Dealer bust") winnings = bet return winnings #Check if player total is less than 21 elif player_total < 21: #Ask player what move they want to make while player_counter == 0: move = input("Move? (h/s) ") print() #Continue dealing cards to player and outputting results for hit if move == 'h': player_card = random.randint(2, 11) player_total += player_card print(name + " received card of value " + str(player_card)) print("Dealer total: " + str(dealer_total)) print(name + " total: " + str(player_total)) print() if player_total >= 21: player_counter = 1 #Stop dealing cards to player for stay elif move == 's': player_counter = 1 #Check if player does not bust if player_total <= 21: #Continue picking cards for dealer if player stays while dealer_counter == 0: dealer_card = random.randint(2, 11) dealer_total += dealer_card print("Dealer received card of value " + str(dealer_card)) print("Dealer total: " + str(dealer_total)) print(name + " total: " + str(player_total)) print() #Pick cards for dealer while his total is less than 17 if dealer_total >= 17: dealer_counter = 1 #Check if dealer ends less than 21 if dealer_total < 21: #Compare dealer total to player total and determine winner if dealer_total < player_total: print(name + " wins") winnings = bet return winnings elif dealer_total > player_total: print("Dealer wins") winnings = -bet return winnings else: print("Push") winnings = 0 return winnings #Check if dealer ends at 21 elif dealer_total == 21: #Compare dealer total to player total and determine winner if dealer_total == player_total: print("Push") winnings = 0 return winnings else: print("Dealer wins") winnings = -bet return winnings #Check if dealer busts else: print("Dealer bust") winnings = bet return winnings #Check if player busts after move elif player_total > 21: print(name + " bust") winnings = -bet return winnings #Check if player busts after initial pick else: print(name + " bust") winnings = -bet return winnings main()