【发布时间】:2021-12-24 23:51:25
【问题描述】:
我用 python 做了一个简单的二十一点游戏。我制作了游戏的其余部分,但我正在努力放入 ascii 卡,所以这只是代码的一小部分。我尝试将 * len(phand) 放在附加行的末尾。虽然这确实给了我多张并排的卡片,但它似乎不允许我编辑它们。
#Phand is the hand you draw so it could be any combination of cards
phand = ["2 of Hearts", "King of Diamonds", "Ace of Clubs"]
print(phand)
for xx in range(0, len(phand)):
pcarddisplay = []
pcarddisplay.append("┌─────────┐")
pcarddisplay.append("│{}{}. . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . {}. .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . . . .│")
pcarddisplay.append("│. . .{}{}│")
pcarddisplay.append("└─────────┘")
x = ("│.", phand[xx][:1], ". . . .│")
pcarddisplay[1] = "".join(x)
x = ("│. . . .", phand[xx][:1], ".│")
pcarddisplay[7] = "".join(x)
if "Diamonds" in phand[xx]:
pcarddisplay[4] = "│. . ♦ . .│"
if "Clubs" in phand[xx]:
pcarddisplay[4] = "│. . ♣ . .│"
if "Hearts" in phand[xx]:
pcarddisplay[4] = "│. . ♥ . .│"
if "Spades" in phand[xx]:
pcarddisplay[4] = "│. . ♠ . .│"
print("\n".join(pcarddisplay))
这个输出:
['2 of Hearts', 'King of Diamonds', 'Ace of Clubs']
┌─────────┐
│.2. . . .│
│. . . . .│
│. . . . .│
│. . ♥ . .│
│. . . . .│
│. . . . .│
│. . . .2.│
└─────────┘
┌─────────┐
│.K. . . .│
│. . . . .│
│. . . . .│
│. . ♦ . .│
│. . . . .│
│. . . . .│
│. . . .K.│
└─────────┘
┌─────────┐
│.A. . . .│
│. . . . .│
│. . . . .│
│. . ♣ . .│
│. . . . .│
│. . . . .│
│. . . .A.│
└─────────┘
我怎样才能将这些并排打印出来?
【问题讨论】:
-
您可以将每一行附加到像
pcarddisplay[4] = pcarddisplay[4] + "│. . X . .│"这样的“保留”变量中,然后一次性打印出来