import random # from art import logo logo = """ .------. _ _ _ _ _ |A_ _ |. | | | | | | (_) | | |( \/ ).-----. | |__ | | __ _ ___| | ___ __ _ ___| | __ | \ /|K /\ | | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ / | \/ | / \ | | |_) | | (_| | (__| <| | (_| | (__| < `-----| \ / | |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\ | \/ K| _/ | `------' |__/ """ deck = [11,2,3,4,5,6,7,8,9,10,10,10] def sum(numbers): total = 0 for num in numbers: total += num return total def reset_hands(): global player_hand global computer_hand player_hand = {"cards" : []} computer_hand = {"cards" : []} while len(player_hand["cards"]) < 2: add_card(player_hand) while len(computer_hand["cards"]) < 2: add_card(computer_hand) display_hands() def display_hands(): print(f"\n Your cards: {player_hand['cards']}, current score: {player_hand['score']}") print(f" Computer's first card: {computer_hand['cards'][0]}\n") def add_card(player): player["cards"].append(random.choice(deck)) player["score"] = sum(player["cards"]) if player["score"] > 21 and 11 in player["cards"]: # print(f"***Ace found!... {player['cards']}") ace = player["cards"].index(11) player["cards"][ace] = 1 player["score"] = sum(player["cards"]) # print(f"***Ace replaced!... {player['cards']}") def show_final_hands(): p_score = player_hand["score"] c_score = computer_hand["score"] while c_score < 17 and p_score < 21: add_card(computer_hand) c_score = computer_hand["score"] if p_score == c_score: result = "Draw" elif p_score > 21: result = "You went over. You lose!" elif c_score > 21: result = "Opponent went over. You win! :)" elif p_score == 21: if len(player_hand["cards"]) == 2: result = "Blackjack... You win!" else: result = "21... You win!" elif c_score == 21: if len(computer_hand["cards"]) == 2: result = "Blackjack... You win!" else: result = "21... You win!" result = "21... You lose!" elif p_score > c_score: result = "You win!" else: result = "You lose!" print(f"\nYour final hand: {player_hand['cards']}, final score: {player_hand['score']}") print(f"Computer's final hand: {computer_hand['cards']}, final score: {computer_hand['score']}") print(f"{result}\n") print("\n\t*********************\n") end_game = False end_round = False first_game = True while not end_game: play_again = input(f"\nDo you want to play a{'' if first_game else 'nother'} game of Blackjack? Type 'y' or 'n': ").lower() first_game = False if play_again == "n": print("Goodbye") end_game = True elif play_again == "y": print(logo) reset_hands() hit = input("Type 'y' to get another card, type 'n' to pass: ").lower() if hit == "n": end_round = True show_final_hands() elif hit == "y": end_round = False while not end_round: add_card(player_hand) display_hands() if player_hand["score"] >= 21: end_round = True show_final_hands() elif player_hand["score"] < 21: hit = input("Type 'y' to get another card, type 'n' to pass: ").lower() if hit == "n": end_round = True show_final_hands() else: end_game = True print("Sorry, invalid response...\nGame over.") else: end_game = True print("Sorry, invalid response...\nGame over.")