英文:
How to format multiple columns of text, each column being left aligned?
问题
我目前正在尝试使用我的Python代码获得这个输出:
Three of Spades Four of Spades Seven of Spades Ten of Spades
Queen of Clubs Three of Clubs King of Diamonds Nine of Spades
Seven of Clubs Jack of Hearts Six of Hearts King of Hearts
Ten of Diamonds Eight of Clubs Three of Hearts Ace of Hearts
Nine of Clubs Eight of Spades Ten of Hearts Nine of Hearts
Jack of Diamonds Five of Clubs King of Spades Ace of Clubs
Deuce of Spades Deuce of Diamonds Queen of Diamonds Six of Spades
Four of Hearts Ace of Diamonds Five of Diamonds Six of Clubs
Jack of Spades Eight of Hearts Ace of Spades Six of Diamonds
Ten of Clubs Deuce of Clubs Deuce of Hearts Five of Spades
Seven of Diamonds King of Clubs Nine of Diamonds Queen of Spades
Four of Clubs Five of Hearts Four of Diamonds Seven of Hearts
Eight of Diamonds Queen of Hearts Three of Diamonds Jack of Clubs
以下是我的代码打印输出:
counter = 0
for value, suit in initialize_deck():
if counter % 4 == 0:
print()
counter += 1
print(f'{value:5} of {suit:9}', end=' ')
(initialize_deck())是我之前在代码中定义的函数,它返回一个洗牌后的元组列表,包含每张卡牌和花色的组合。
这是我成功获得的最佳输出:
Four of Spades Five of Hearts Queen of Diamonds Six of Clubs
Ace of Hearts Ace of Spades Eight of Diamonds Nine of Diamonds
King of Diamonds King of Clubs Jack of Spades Deuce of Hearts
King of Hearts Eight of Spades Five of Diamonds Seven of Diamonds
Ten of Clubs King of Spades Three of Hearts Seven of Clubs
Deuce of Clubs Jack of Diamonds Eight of Clubs Seven of Spades
Eight of Hearts Queen of Clubs Six of Spades Ten of Diamonds
Ace of Diamonds Four of Diamonds Five of Clubs Jack of Clubs
Nine of Spades Jack of Hearts Ten of Spades Queen of Spades
Nine of Clubs Deuce of Diamonds Six of Diamonds Queen of Hearts
Nine of Hearts Three of Clubs Seven of Hearts Ten of Hearts
Five of Spades Four of Hearts Six of Hearts Deuce of Spades
Ace of Clubs Four of Clubs Three of Diamonds Three of Spades
我不想在文本段之间有任何间隔,但我希望每一列都能对齐。
英文:
I am currently trying to get this output with my python code:
Three of Spades Four of Spades Seven of Spades Ten of Spades
Queen of Clubs Three of Clubs King of Diamonds Nine of Spades
Seven of Clubs Jack of Hearts Six of Hearts King of Hearts
Ten of Diamonds Eight of Clubs Three of Hearts Ace of Hearts
Nine of Clubs Eight of Spades Ten of Hearts Nine of Hearts
Jack of Diamonds Five of Clubs King of Spades Ace of Clubs
Deuce of Spades Deuce of Diamonds Queen of Diamonds Six of Spades
Four of Hearts Ace of Diamonds Five of Diamonds Six of Clubs
Jack of Spades Eight of Hearts Ace of Spades Six of Diamonds
Ten of Clubs Deuce of Clubs Deuce of Hearts Five of Spades
Seven of Diamonds King of Clubs Nine of Diamonds Queen of Spades
Four of Clubs Five of Hearts Four of Diamonds Seven of Hearts
Eight of Diamonds Queen of Hearts Three of Diamonds Jack of Clubs
Here is my code printing the output:
counter = 0
for value, suit in initialize_deck():
if counter % 4 == 0:
print()
counter += 1
print(f'{value:5} of {suit:9}', end=' ')
(initialize_deck()) is a function previously defined earlier in my code, which returns a shuffled list of tuples containing each card and suit combination.
This is the best output I have managed to get:
Four of Spades Five of Hearts Queen of Diamonds Six of Clubs
Ace of Hearts Ace of Spades Eight of Diamonds Nine of Diamonds
King of Diamonds King of Clubs Jack of Spades Deuce of Hearts
King of Hearts Eight of Spades Five of Diamonds Seven of Diamonds
Ten of Clubs King of Spades Three of Hearts Seven of Clubs
Deuce of Clubs Jack of Diamonds Eight of Clubs Seven of Spades
Eight of Hearts Queen of Clubs Six of Spades Ten of Diamonds
Ace of Diamonds Four of Diamonds Five of Clubs Jack of Clubs
Nine of Spades Jack of Hearts Ten of Spades Queen of Spades
Nine of Clubs Deuce of Diamonds Six of Diamonds Queen of Hearts
Nine of Hearts Three of Clubs Seven of Hearts Ten of Hearts
Five of Spades Four of Hearts Six of Hearts Deuce of Spades
Ace of Clubs Four of Clubs Three of Diamonds Three of Spades
I don't want any gaps between the segments of text, but I want each colum to be in a nice line.
答案1
得分: 2
You need to first create a string containing {value} of {suit}
, and then format this string to the required width.
for counter, (value, suit) in enumerate(initialize_deck()):
if counter % 4 == 0:
print()
card = f'{value} of {suit}'
print(f'{card:18}', end=' ')
This gives your desired output:
Three of Spades Four of Spades Seven of Spades Ten of Spades
Queen of Clubs Three of Clubs King of Diamonds Nine of Spades
Seven of Clubs Jack of Hearts Six of Hearts King of Hearts
Ten of Diamonds Eight of Clubs Three of Hearts Ace of Hearts
Nine of Clubs Eight of Spades Ten of Hearts Nine of Hearts
Jack of Diamonds Five of Clubs King of Spades Ace of Clubs
Deuce of Spades Deuce of Diamonds Queen of Diamonds Six of Spades
Four of Hearts Ace of Diamonds Five of Diamonds Six of Clubs
Jack of Spades Eight of Hearts Ace of Spades Six of Diamonds
Ten of Clubs Deuce of Clubs Deuce of Hearts Five of Spades
Seven of Diamonds King of Clubs Nine of Diamonds Queen of Spades
Four of Clubs Five of Hearts Four of Diamonds Seven of Hearts
Eight of Diamonds Queen of Hearts Three of Diamonds Jack of Clubs
Note that I used enumerate
to count the iterations instead of maintaining a counter manually, but that is irrelevant to formatting the text.
英文:
You need to first create a string containing {value} of {suit}
, and then format this string to the required width.
for counter, (value, suit) in enumerate(initialize_deck()):
if counter % 4 == 0:
print()
card = f'{value} of {suit}'
print(f'{card:18}', end=' ')
This gives your desired output:
Three of Spades Four of Spades Seven of Spades Ten of Spades
Queen of Clubs Three of Clubs King of Diamonds Nine of Spades
Seven of Clubs Jack of Hearts Six of Hearts King of Hearts
Ten of Diamonds Eight of Clubs Three of Hearts Ace of Hearts
Nine of Clubs Eight of Spades Ten of Hearts Nine of Hearts
Jack of Diamonds Five of Clubs King of Spades Ace of Clubs
Deuce of Spades Deuce of Diamonds Queen of Diamonds Six of Spades
Four of Hearts Ace of Diamonds Five of Diamonds Six of Clubs
Jack of Spades Eight of Hearts Ace of Spades Six of Diamonds
Ten of Clubs Deuce of Clubs Deuce of Hearts Five of Spades
Seven of Diamonds King of Clubs Nine of Diamonds Queen of Spades
Four of Clubs Five of Hearts Four of Diamonds Seven of Hearts
Eight of Diamonds Queen of Hearts Three of Diamonds Jack of Clubs
Note that I used enumerate
to count the iterations instead of maintaining a counter manually, but that is irrelevant to formatting the text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论