Padding strings for a clean visual print out...

Joined
Jul 20, 2023
Messages
56
Reaction score
2
I'm trying to print several lines to the console including variables with lengths that will change and I want to keep spacing correct and clean and easy to read. This is how I always like to write my console programs. Soon I'll to start writing with GUI's but for now this is still where I'm at. Here is where I'm at so far. It looks terrible on the code side and is not yet dynamic to the changing var lengths. I know I can rewrite it to do that but will only make it even messier and Im already having an issue running it. It works in Pycharm but when I run it in the terminal it says "SyntaxError: f-string expression part cannot include a backslash".

Python:
# BLACKJACK SAMPLE
from random import shuffle

card_numbers = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10',  'J', 'Q', 'K']
suits = ["hearts", "clubs", "spades", "diamonds"]
suits_abbreviated = ["H", "C", "S", "D"]
cards = [f'{card_num}-{suit}' for suit in suits_abbreviated for card_num in card_numbers]
shuffle(cards)

players_wallet = 500
dealers_wallet = 500
players_hand = []
dealers_hand = []
players_hand_value = 16
dealers_hand_value = 20
players_bid = 100
dealers_bid = 100

[players_hand.append(cards.pop()) for i in range(2)]
[dealers_hand.append(cards.pop()) for i in range(2)]

while True:
    game_stats = f"""
    {'\t' * 5}Your stats:{'\t' * 14}Dealer stats:
    {'\t' * 4}Your hand: {players_hand}{'\t' * 9}Dealers hand: {dealers_hand}
    {'\t' * 4}Your hand value: {players_hand_value}{'\t' * 11}Dealers hand value: {dealers_hand_value}
    {'\t' * 4}Your wallet: ${players_wallet}{'\t' * 11}Dealers wallet: ${dealers_wallet}\n
    {'\t' * 4}Your bid: ${players_bid}{'\t' * 11}Dealers Bid: ${dealers_bid}"""

    print(game_stats)

    players_hand.append(cards.pop())
    dealers_hand.append(cards.pop())
  
    ans = input("Press X to exit: ")

    if ans.lower() == 'x':
        break

I'm thinking a better way is to create a list containing each line and making sure the the first half length is equal to a certain value like 40 but I'm not really sure how I can do that.

Any suggestions on the best way to go about this?
Is it going to look ugly no matter what?
 
Last edited:
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Ya so I came up with this and though it does work I really dont like it. In fact I would say its far worse than before.
Python:
# BLACKJACK SAMPLE
from random import shuffle

card_numbers = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10',  'J', 'Q', 'K']
suits = ["hearts", "clubs", "spades", "diamonds"]
suits_abbreviated = ["H", "C", "S", "D"]
cards = [f'{card_num}-{suit}' for suit in suits_abbreviated for card_num in card_numbers]
shuffle(cards)

players_wallet = 500
dealers_wallet = 500
players_hand = []
dealers_hand = []
players_hand_value = 16
dealers_hand_value = 20
players_bid = 100
dealers_bid = 100

[players_hand.append(cards.pop()) for i in range(2)]
[dealers_hand.append(cards.pop()) for i in range(2)]

while True:
    lines = []
    lines.append(f'{'\t' * 5}Your stats:{'\t' * 14}Dealer stats:')
    lines.append(f'{'\t' * 4}Your hand: {players_hand}')
    lines[len(lines)-1] += ' ' * (70 - len(lines[len(lines)-1])) + f'Dealers hand: {dealers_hand}'
    lines.append(f'{'\t' * 4}Your hand value: {players_hand_value}')
    lines[len(lines)-1] += ' ' * (70 - len(lines[len(lines)-1])) + f'Dealers hand: {dealers_hand_value}'
    lines.append(f'{'\t' * 4}Your wallet: ${players_wallet}')
    lines[len(lines)-1] += ' ' * (70 - len(lines[len(lines)-1])) + f'Dealers wallet: ${dealers_wallet}'
    lines.append('')
    lines.append(f'{'\t' * 4}Your bid: ${players_bid}')
    lines[len(lines)-1] += ' ' * (70 - len(lines[len(lines)-1])) + f'Dealers Bid: ${dealers_bid}'

    for line in lines:
        print(line)

    players_hand.append(cards.pop())
    dealers_hand.append(cards.pop())

    ans = input("\n\nPress X to exit: ")

    print('\n' * 4)

    if ans.lower() == 'x':
        break

Please let me know if you have a better solution
 
Joined
Dec 10, 2022
Messages
94
Reaction score
26
Here is one way to consider

Python:
class Player:
    def __init__(self, player):
        self.name = player
        self.wallet = 500
        self.bid= 100
        self.hand_value = 16
        self.hand = [2, 6]
 


class Display:
    def display(self, player):
        return(
            f'''
                {player.name.capitalize()}\'s Stats
                Wallet: {player.wallet}
                Bid: {player.bid}
                Hand: {player.hand}
                Hand Value: {player.hand_value}
            '''
        )


player = Player(player='john')
player.hand = [9, 10]

player2 = Player(player='PC')
player2.bid = 75
player2.hand_value = 21

show = Display()
p1 = show.display(player).split('\n')
p2 = show.display(player2).split('\n')

p1 = [data.strip() for data in p1 if data.strip()]
p2 = [data.strip() for data in p2 if data.strip()]

data = '\n'.join('{}\t\t{}'.format(x,y) for x,y in zip(p1,p2))


print(data)

Output
Code:
John's Stats'           Pc's Stats'
Wallet: 500             Wallet: 500
Bid: 100                Bid: 75
Hand: [9, 10]           Hand: [2, 6]
Hand Value: 16          Hand Value: 21
 
Joined
Dec 22, 2023
Messages
34
Reaction score
5
Here is one way to consider

Python:
class Player:
    def __init__(self, player):
        self.name = player
        self.wallet = 500
        self.bid= 100
        self.hand_value = 16
        self.hand = [2, 6]
 


class Display:
    def display(self, player):
        return(
            f'''
                {player.name.capitalize()}\'s Stats'
                Wallet: {player.wallet}
                Bid: {player.bid}
                Hand: {player.hand}
                Hand Value: {player.hand_value}
            '''
        )


player = Player(player='john')
player.hand = [9, 10]

player2 = Player(player='PC')
player2.bid = 75
player2.hand_value = 21

show = Display()
p1 = show.display(player).split('\n')
p2 = show.display(player2).split('\n')

p1 = [data.strip() for data in p1 if data.strip()]
p2 = [data.strip() for data in p2 if data.strip()]

data = '\n'.join('{}\t\t{}'.format(x,y) for x,y in zip(p1,p2))


print(data)

Output
Code:
John's Stats'           Pc's Stats'
Wallet: 500             Wallet: 500
Bid: 100                Bid: 75
Hand: [9, 10]           Hand: [2, 6]
Hand Value: 16          Hand Value: 21
yeah thats good : )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top