How to print cards side by side

63 views Asked by At

HERE IS MY CODE BELOW. Ive tried multiple different things and asked in discord servers but no one has been able to respond or help me.

#import modules
import random
import os
import time

#global variables
random_suit = ['♠','♥','♦','♣']
random_card = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
lettered_card = ['A', 'J', 'Q', 'K']

dealer_sum = 0
user_sum = 0

dealer_cards = []
dealer_suit = []

turns = 0

user_cards = []
user_suit = []

user_balance = og_balance = 1000.00
user_bet = 0


# Print cards
def card_print(chosen_suit,chosen_card):
    print(f'*------*')
    print(f'| {chosen_card:<2}   |')
    print(f'|      |')
    print(f'|    {chosen_suit} |')
    print(f'*------*')


#run
def main():
    os.system('cls')
    print('----------------------')
    print('Welcome to Blackjack!')
    bets()
    user_hand(2)
    time.sleep(1)
    dealer_hand()
    time.sleep(1)
    check_sum()
    hit_stay()

#bets
def bets():
    global user_bet
    global user_balance
    print(f'Your balance is currently: ${user_balance}')
    bet = int(input('How much would you like to bet?: $'))
    if bet < 0 or bet < 10:
        print('Minimum bet is $10')
        time.sleep(1)
        main()
    if bet > user_balance:
        print('Not enough money in balance')
        time.sleep(1)
        main()
    elif bet < user_balance:
        user_bet += bet
    else:
        print('Invalid Session.')
        time.sleep(1)
        main()

# Print dealer hand
def dealer_hand():
    global dealer_sum
    print('-------------')
    print('Dealer Hand: ')
    time.sleep(1)
    chosen_card = random.choice(random_card)
    chosen_suit = random.choice(random_suit)

    dealer_cards.append(str(chosen_card))
    dealer_suit.append(str(chosen_suit))

    dealer_sum += check_letters(chosen_card)
    card_print(chosen_suit,chosen_card)
    time.sleep(1)
    print(f'*------*')
    print(f'| ???? |')
    print(f'| ???? |')
    print(f'| ???? |')
    print(f'*------*')
    print('Dealer Total: ', dealer_sum)

# Print user hand
def user_hand(amount):
    global user_sum
    print('-------------')
    print('Your Hand: ')
    for i in range(amount):
        time.sleep(1)
        chosen_card = random.choice(random_card)
        chosen_suit = random.choice(random_suit)

        user_cards.append(str(chosen_card))
        user_suit.append(str(chosen_suit))

        user_sum += check_letters(chosen_card)
        card_print(chosen_suit, chosen_card)
    print('Your Total: ', user_sum)

# Convert letters to numbers
def check_letters(chosen_card):
    if chosen_card.isdigit():
        return int(chosen_card)
    else:
        for _ in lettered_card:
            if chosen_card != 'A':
                return 10
            else:
                return 11

# Check if the total of the dealer or user is over 21
def check_sum():
    global user_bet
    global user_balance
    def win(bj):
        global user_bet
        global user_balance
        if bj:
            user_bet_won = user_bet * 1.75
            user_balance += user_bet_won
        else:
            user_bet_won = user_bet * 1.25
            user_balance += user_bet_won

    def lose():
        global user_bet
        global user_balance
        user_bet_lost = user_bet * 1.25
        user_balance -= user_bet_lost

    if user_sum > 21:
        print('-------------')
        print('Your over 21! You lose!')
        lose()
        play_again()
    elif dealer_sum > 21:
        print('-------------')
        print('Dealer over 21! You win!')
        win(False)
        play_again()
    elif user_sum == 21:
        print('-------------')
        print('You have Blackjack. You win!')
        win(True)
        play_again()
    elif dealer_sum == 21:
        print('-------------')
        print('Dealer has blackjack. You lose!')
        lose()
        play_again()
    elif dealer_sum > user_sum and turns > 0:
        print('-------------')
        print('Dealer has more. You lose!')
        lose()
        play_again()
    else:
        pass

# Hit or Knock
def hit_stay():
    global turns
    turns += 1
    def hit():
        global user_sum
        global dealer_sum
        os.system('cls')
        print('-------------')
        print('HIT')
        #print('P Cards:', user_cards, user_suit)
        #print('D Cards:', dealer_cards, dealer_suit)

        #your hand
        print('-------------')
        print('Your Hand: ')
        for i_index, card in enumerate(user_cards):
            card_print(user_suit[i_index], card)

        time.sleep(1)
        chosen_card = random.choice(random_card)
        chosen_suit = random.choice(random_suit)

        user_cards.append(str(chosen_card))
        user_suit.append(str(chosen_suit))

        user_sum += check_letters(chosen_card)
        card_print(chosen_suit, chosen_card)
        print('Your Total: ', user_sum)

        # dealer hand
        print('-------------')
        print('Dealer Hand: ')
        for i_index, card in enumerate(dealer_cards):
            card_print(dealer_suit[i_index], card)

        if dealer_sum >= 17:
            check_sum()
            hit_stay()
        elif dealer_sum < 17:
            time.sleep(1)
            chosen_card = random.choice(random_card)
            chosen_suit = random.choice(random_suit)

            dealer_cards.append(str(chosen_card))
            dealer_suit.append(str(chosen_suit))

            dealer_sum += check_letters(chosen_card)
            card_print(chosen_suit, chosen_card)
            print('Dealer Total: ', dealer_sum)
            check_sum()
            hit_stay()



    def stay():
        global dealer_sum
        os.system('cls')
        print('-------------')
        print('STAY')
        #print('P Cards:', user_cards, user_suit)
        #print('D Cards:', dealer_cards, dealer_suit)

        # your hand
        print('-------------')
        print('Your Hand:')
        for i_index, card in enumerate(user_cards):
            card_print(user_suit[i_index], card)
        print('Your Total: ', user_sum)
        #print('Knock Function Executed')

        # dealer hand
        print('-------------')
        print('Dealer Hand: ')
        for i_index, card in enumerate(dealer_cards):
            card_print(dealer_suit[i_index], card)

        if dealer_sum >= 17:
            check_sum()
            hit_stay()
        elif dealer_sum < 17:
            time.sleep(1)
            chosen_card = random.choice(random_card)
            chosen_suit = random.choice(random_suit)

            dealer_cards.append(str(chosen_card))
            dealer_suit.append(str(chosen_suit))

            dealer_sum += check_letters(chosen_card)
            card_print(chosen_suit, chosen_card)
            print('Dealer Total: ', dealer_sum)
            check_sum()
            hit_stay()


    #outside of hit and knock function
    print('-------------')
    user_choice = str(input('Hit or Stay?(h/s): '))
    if user_choice == 'h':
        hit()
    elif user_choice == 's':
        stay()

def play_again():
    global dealer_sum
    global user_sum
    global dealer_cards
    global dealer_suit
    global turns
    global user_cards
    global user_suit
    global user_bet
    print(f'Balance: ${user_balance}')
    pG = str(input('Would you like to play again?(y/n): '))
    if pG == 'y':
        dealer_sum = 0
        user_sum = 0

        dealer_cards = []
        dealer_suit = []

        turns = 0

        user_cards = []
        user_suit = []

        user_bet = 0

        main()
    else:
        exit()


if __name__ == '__main__':
    main()

enter image description here The cards are only printing under one another. I am a beginner in python. I started maybe 1 and a half weeks ago so please dont flame me for my code not being the most effiencent. Thanks

2

There are 2 answers

3
001 On

You could put the cards into an list and print them one line at a time:

def print_hand(hand):
    # Print first line of all cards in hand
    for card in hand:
        print("*------*", end=' ')
    print()
    # 2nd line
    for card in hand:
        print(f'| {card[0]:<2}   |', end=' ')
    print()
    # 3rd line
    for card in hand:
        print("|      |", end=' ')
    print()
    # 4th line
    for card in hand:
        print(f"|    {card[1]} |", end=' ')
    print()
    # Last line
    for card in hand:
        print("*------*", end=' ')

hand = [('A', '♥'), ('K', '♦'), ('4', '♣')]
print_hand(hand)

Output:

*------* *------* *------*  
| A    | | K    | | 4    | 
|      | |      | |      | 
|    ♥ | |    ♦ | |    ♣ | 
*------* *------* *------* 
0
ExpressMpeg On

Here's a solution to print cards in the same row:

#import modules
from calendar import c
import random
import os
import time
from collections import namedtuple

#global variables
random_suit = ['♠','♥','♦','♣']
random_card = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
lettered_card = ['A', 'J', 'Q', 'K']

dealer_sum = 0
user_sum = 0

dealer_cards = []
dealer_suit = []

turns = 0

user_cards = []
user_suit = []

user_balance = og_balance = 1000.00
user_bet = 0

Card = namedtuple("Card", ["chosen_suit", "chosen_card"])
DEALER_CARD = ( f'*------*',
                f'| ???? |',
                f'| ???? |',
                f'| ???? |',
                f'*------*',
)
INDENT_SPACE = "   "

# Print cards
def card_print(*cards: Card):
    for c in cards:
        if c == DEALER_CARD:
            print(c[0], end="")
            continue
        print(f'*------*'+INDENT_SPACE, end="")
    print()
    for c in cards:
        if c == DEALER_CARD:
            print(c[1], end="")
            continue
        print(f'| {c.chosen_card:<2}   |'+INDENT_SPACE, end="")
    print()
    for c in cards:
        if c == DEALER_CARD:
            print(c[2], end="")
            continue
        print(f'|      |'+INDENT_SPACE, end="")
    print()
    for c in cards:
        if c == DEALER_CARD:
            print(c[3], end="")
            continue
        print(f'|    {c.chosen_suit} |'+INDENT_SPACE, end="")
    print()
    for c in cards:
        if c == DEALER_CARD:
            print(c[4])
            continue
        print(f'*------*'+INDENT_SPACE, end="")
    print()


#run
def main():
    os.system('cls')
    print('----------------------')
    print('Welcome to Blackjack!')
    bets()
    user_hand(2)
    time.sleep(1)
    dealer_hand()
    time.sleep(1)
    check_sum()
    hit_stay()

#bets
def bets():
    global user_bet
    global user_balance
    print(f'Your balance is currently: ${user_balance}')
    bet = int(input('How much would you like to bet?: $'))
    if bet < 0 or bet < 10:
        print('Minimum bet is $10')
        time.sleep(1)
        main()
    if bet > user_balance:
        print('Not enough money in balance')
        time.sleep(1)
        main()
    elif bet < user_balance:
        user_bet += bet
    else:
        print('Invalid Session.')
        time.sleep(1)
        main()

# Print dealer hand
def dealer_hand():
    global dealer_sum
    print('-------------')
    print('Dealer Hand: ')
    time.sleep(1)
    chosen_card = random.choice(random_card)
    chosen_suit = random.choice(random_suit)

    dealer_cards.append(str(chosen_card))
    dealer_suit.append(str(chosen_suit))

    dealer_sum += check_letters(chosen_card)
    card_print(Card(chosen_suit,chosen_card), DEALER_CARD)
    time.sleep(1)
    print('Dealer Total: ', dealer_sum)

# Print user hand
def user_hand(amount):
    global user_sum
    print('-------------')
    print('Your Hand: ')
    cards_to_print = []
    for i in range(amount):
        time.sleep(1)
        chosen_card = random.choice(random_card)
        chosen_suit = random.choice(random_suit)

        user_cards.append(str(chosen_card))
        user_suit.append(str(chosen_suit))

        user_sum += check_letters(chosen_card)
        cards_to_print.append(Card(chosen_suit, chosen_card))
    card_print(*cards_to_print)
    print('Your Total: ', user_sum)

# Convert letters to numbers
def check_letters(chosen_card):
    if chosen_card.isdigit():
        return int(chosen_card)
    else:
        for _ in lettered_card:
            if chosen_card != 'A':
                return 10
            else:
                return 11

# Check if the total of the dealer or user is over 21
def check_sum():
    global user_bet
    global user_balance
    def win(bj):
        global user_bet
        global user_balance
        if bj:
            user_bet_won = user_bet * 1.75
            user_balance += user_bet_won
        else:
            user_bet_won = user_bet * 1.25
            user_balance += user_bet_won

    def lose():
        global user_bet
        global user_balance
        user_bet_lost = user_bet * 1.25
        user_balance -= user_bet_lost

    if user_sum > 21:
        print('-------------')
        print('Your over 21! You lose!')
        lose()
        play_again()
    elif dealer_sum > 21:
        print('-------------')
        print('Dealer over 21! You win!')
        win(False)
        play_again()
    elif user_sum == 21:
        print('-------------')
        print('You have Blackjack. You win!')
        win(True)
        play_again()
    elif dealer_sum == 21:
        print('-------------')
        print('Dealer has blackjack. You lose!')
        lose()
        play_again()
    elif dealer_sum > user_sum and turns > 0:
        print('-------------')
        print('Dealer has more. You lose!')
        lose()
        play_again()
    else:
        pass

# Hit or Knock
def hit_stay():
    global turns
    turns += 1
    def hit():
        global user_sum
        global dealer_sum
        os.system('cls')
        print('-------------')
        print('HIT')
        #print('P Cards:', user_cards, user_suit)
        #print('D Cards:', dealer_cards, dealer_suit)

        #your hand
        print('-------------')
        print('Your Hand: ')
        cards_to_print = []
        for i_index, card in enumerate(user_cards):
            cards_to_print.append(Card(user_suit[i_index], card))
        card_print(*cards_to_print)

        time.sleep(1)
        chosen_card = random.choice(random_card)
        chosen_suit = random.choice(random_suit)

        user_cards.append(str(chosen_card))
        user_suit.append(str(chosen_suit))

        user_sum += check_letters(chosen_card)
        card_print(Card(chosen_suit, chosen_card))
        print('Your Total: ', user_sum)

        # dealer hand
        print('-------------')
        print('Dealer Hand: ')
        cards_to_print = []
        for i_index, card in enumerate(dealer_cards):
            cards_to_print.append(Card(dealer_suit[i_index], card))
        card_print(*cards_to_print)

        if dealer_sum >= 17:
            check_sum()
            hit_stay()
        elif dealer_sum < 17:
            time.sleep(1)
            chosen_card = random.choice(random_card)
            chosen_suit = random.choice(random_suit)

            dealer_cards.append(str(chosen_card))
            dealer_suit.append(str(chosen_suit))

            dealer_sum += check_letters(chosen_card)
            card_print(Card(chosen_suit, chosen_card))
            print('Dealer Total: ', dealer_sum)
            check_sum()
            hit_stay()



    def stay():
        global dealer_sum
        os.system('cls')
        print('-------------')
        print('STAY')
        #print('P Cards:', user_cards, user_suit)
        #print('D Cards:', dealer_cards, dealer_suit)

        # your hand
        print('-------------')
        print('Your Hand:')
        cards_to_print = []
        for i_index, card in enumerate(user_cards):
            cards_to_print.append(Card(user_suit[i_index], card))
        card_print(*cards_to_print)
        print('Your Total: ', user_sum)
        #print('Knock Function Executed')

        # dealer hand
        print('-------------')
        print('Dealer Hand: ')
        cards_to_print = []
        for i_index, card in enumerate(dealer_cards):
            cards_to_print.append(Card(dealer_suit[i_index], card))
        card_print(*cards_to_print)
        

        if dealer_sum >= 17:
            check_sum()
            hit_stay()
        elif dealer_sum < 17:
            time.sleep(1)
            chosen_card = random.choice(random_card)
            chosen_suit = random.choice(random_suit)

            dealer_cards.append(str(chosen_card))
            dealer_suit.append(str(chosen_suit))

            dealer_sum += check_letters(chosen_card)
            card_print(Card(chosen_suit, chosen_card))
            print('Dealer Total: ', dealer_sum)
            check_sum()
            hit_stay()


    #outside of hit and knock function
    print('-------------')
    user_choice = str(input('Hit or Stay?(h/s): '))
    if user_choice == 'h':
        hit()
    elif user_choice == 's':
        stay()

def play_again():
    global dealer_sum
    global user_sum
    global dealer_cards
    global dealer_suit
    global turns
    global user_cards
    global user_suit
    global user_bet
    print(f'Balance: ${user_balance}')
    pG = str(input('Would you like to play again?(y/n): '))
    if pG == 'y':
        dealer_sum = 0
        user_sum = 0

        dealer_cards = []
        dealer_suit = []

        turns = 0

        user_cards = []
        user_suit = []

        user_bet = 0

        main()
    else:
        exit()


if __name__ == '__main__':
    main()

To make this possible, I used collections.namedtuple to order the cards data. This simplifies the access of the data in the card_print function and adds clarity to the code.

The DEALER_CARD constant has been added to print the card in the card_print function.