Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.2 kB
4
Indexable
import art
import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
play = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")
indent = " " * 4 # indent for printing

def add_cards(card_list):
    total = 0
    for x in card_list:
        total += x
    return total

def over_21(card_list):
    if add_cards(card_list) > 21:
        if 11 not in card_list:
            print(indent + f"Your cards: {card_list}, current score: {add_cards(card_list)}")
            print("Bust!")
            quit()
        elif 11 in card_list:
            position = card_list.index(11)
            card_list[position] = 1


if play == 'y':
    print(art.logo)
    players_cards = [random.choice(cards), random.choice(cards)]
    players_cards_total = add_cards(players_cards)

    computers_cards = [random.choice(cards)]
    computers_cards_total = computers_cards[0]
    print(indent + f"Your cards: {players_cards}, current score: {players_cards_total}")
    print(indent + f"Computer's first card: {computers_cards[0]}")

    hit = True
    while hit:
        extra_card = input("\nType 'y' to get another card, type 'n' to pass: ")

        if extra_card == 'y':
            players_cards.append(random.choice(cards))
            over_21(players_cards)

            print(indent + f"Your cards: {players_cards}, current score: {add_cards(players_cards)}")
            print(indent + f"Computer's first card: {computers_cards[0]}")
        else:
            hit = False
            print(indent + f"Your final hand: {players_cards}, final score: {add_cards(players_cards)}")
            while computers_cards_total < 17:
                computers_cards.append(random.choice(cards))
                computers_cards_total = add_cards(computers_cards)

            print(indent + f"Computer's final hand: {computers_cards}, final score: {add_cards(computers_cards)}")

            if add_cards(computers_cards) > 21:
                print("\nYou win!")
                quit()

    if add_cards(players_cards) > add_cards(computers_cards):
        print("\nYou Win!")
    else:
        print("\nYou lose.")
Leave a Comment