Python 如何在输出窗口中并排打印单独的多行ascii符号
免责声明:一般来说,我是一个相对较新的python用户和程序员 我正在为一组卡片构建一个类,对于Python 如何在输出窗口中并排打印单独的多行ascii符号,python,logic,Python,Logic,免责声明:一般来说,我是一个相对较新的python用户和程序员 我正在为一组卡片构建一个类,对于\uuuu str\uuuu方法,我希望将当前在卡片组中的卡片的ascii符号返回为十三行四列。稍后,当我实际使用这个类进行游戏时,在显示玩家的手时,我将需要类似的逻辑。我希望找到一种方法来做到这一点,其中列的数量是可变的,行的数量取决于列的数量和列表的长度(或者简单地说,当卡片用完时停止)。这样,我的\uuuu str\uuuu返回就可以了,它有4列,玩家的手可以控制不同的列数 因为我只想理解这样做
\uuuu str\uuuu
方法,我希望将当前在卡片组中的卡片的ascii符号返回为十三行四列。稍后,当我实际使用这个类进行游戏时,在显示玩家的手时,我将需要类似的逻辑。我希望找到一种方法来做到这一点,其中列的数量是可变的,行的数量取决于列的数量和列表的长度(或者简单地说,当卡片用完时停止)。这样,我的\uuuu str\uuuu
返回就可以了,它有4列,玩家的手可以控制不同的列数
因为我只想理解这样做的逻辑,所以我将问题简化为下面的代码。我已经做了很多研究,但是我还没有找到一个我能理解或者不使用导入库的例子。我已经学会在print语句后使用逗号来防止强制换行,但即使使用该工具,我也无法找到使用for和while循环的方法。我还将粘贴来自最终用例的一些代码。这只是许多没有成功的例子,可能很可怕,但这正是我所处的位置
简化用例:
# Each string in each list below would actually be one line of ascii art for
# the whole card, an example would be '|{v} {s} |'
deck = [['1','2','3','4'],
['5','6','7','8'],
['9','10','11','12'],
['a','b','c','d'],
['e','f','g','h'],
['i','j','k','l']]
# expected output in 3 columns:
#
# 1 5 9
# 2 6 10
# 3 7 11
# 4 8 12
#
# a e i
# b f j
# c g k
# d h l
#
# expected output in 4 columns:
#
# 1 5 9 a
# 2 6 10 b
# 3 7 11 c
# 4 8 12 d
#
# e i
# f j
# g k
# h l
def __str__(self):
# WORKS empty list to hold ascii strings
built_deck = []
# WORKS fill the list with ascii strings [card1,card2,card3,card4...]
for card in self.deck:
built_deck.append(self.build_card(str(card[0]),str(card[1:])))
# WORKS transform the list to [allCardsRow1,allCardsRow2,allCardsRow3,allCardsRow4...]
built_deck = list(zip(*built_deck))
# mark first column as position
position = 1
# initialize position to beginning of deck
card = 0
# Try to print the table of cards ***FAILURE***
for item in built_deck:
while position <= 4:
print(f'{item[card]}\t',)
card += 1
continue
position = 1
print(f'{item[card]}')
card += 1
#return built_deck
最终用例:
# Each string in each list below would actually be one line of ascii art for
# the whole card, an example would be '|{v} {s} |'
deck = [['1','2','3','4'],
['5','6','7','8'],
['9','10','11','12'],
['a','b','c','d'],
['e','f','g','h'],
['i','j','k','l']]
# expected output in 3 columns:
#
# 1 5 9
# 2 6 10
# 3 7 11
# 4 8 12
#
# a e i
# b f j
# c g k
# d h l
#
# expected output in 4 columns:
#
# 1 5 9 a
# 2 6 10 b
# 3 7 11 c
# 4 8 12 d
#
# e i
# f j
# g k
# h l
def __str__(self):
# WORKS empty list to hold ascii strings
built_deck = []
# WORKS fill the list with ascii strings [card1,card2,card3,card4...]
for card in self.deck:
built_deck.append(self.build_card(str(card[0]),str(card[1:])))
# WORKS transform the list to [allCardsRow1,allCardsRow2,allCardsRow3,allCardsRow4...]
built_deck = list(zip(*built_deck))
# mark first column as position
position = 1
# initialize position to beginning of deck
card = 0
# Try to print the table of cards ***FAILURE***
for item in built_deck:
while position <= 4:
print(f'{item[card]}\t',)
card += 1
continue
position = 1
print(f'{item[card]}')
card += 1
#return built_deck
def\uuuu str\uuuuuu(自):
#使用空列表来保存ascii字符串
已建甲板=[]
#WORKS使用ascii字符串填充列表[card1、card2、card3、card4…]
对于self.deck中的卡:
build_-deck.append(self.build_-card(str(card[0]),str(card[1:]))
#WORKS将列表转换为[allCardsRow1、allCardsRow2、allCardsRow3、allCardsRow4…]
已建甲板=列表(zip(*已建甲板))
#将第一列标记为位置
位置=1
#将位置初始化为甲板的起始位置
卡=0
#尝试打印卡片表***失败***
对于内置甲板上的物品:
而position的诀窍是要认识到,你正在做的是对你的卡片矩阵进行连续的转置,然后打印它们,而你执行操作的矩阵的大小就是你想要显示的项目的数量。我们可以使用python中的zip
获得转置
def display(deck, number_of_columns):
col = 0
while col < len(deck):
temp = deck[col:col+number_of_columns]
temp = zip(*temp)
for x in temp:
for y in x:
print(y, end=" ")
print()
col += number_of_columns
display(deck, 3)
print()
display(deck, 4)