Python卡片打印机
我不明白为什么卡片没有第二次打印,即使字符串“玩家的手”再次打印。有人能帮我找到再次打印它们的解决方案吗? 为之前的错误感到抱歉,我刚刚全部更正Python卡片打印机,python,python-3.x,user-defined-functions,ascii-art,Python,Python 3.x,User Defined Functions,Ascii Art,我不明白为什么卡片没有第二次打印,即使字符串“玩家的手”再次打印。有人能帮我找到再次打印它们的解决方案吗? 为之前的错误感到抱歉,我刚刚全部更正 def printCards(playerHand): a = playerHand print("Player's Hand") while a[0]: for i in range(0,len(a)): card = a[i] print(card[:11] +
def printCards(playerHand):
a = playerHand
print("Player's Hand")
while a[0]:
for i in range(0,len(a)):
card = a[i]
print(card[:11] + " ", sep = " ", end = "")
a[i] = card[12:]
print("")
print("*****************************")
return
playerHand = [""" _________
| |
| A |
| |
| ♣ |
| |
| A |
|_________|""",
""" _________
| |
| 2 |
| |
| ♣ |
| |
| 2 |
|_________|""",
""" _________
| |
| 3 |
| |
| ♣ |
| |
| 3 |
|_________|"""]
printCards(playerHand)
print(" -----")
#Why are the cards not getting printed second time with the command below?
printCards(playerHand)
如果您将每张卡分成几行,并同时打印相同数字的行,您的问题可以以更优雅的方式得到解决:
def printCards(playerHand):
# Prepare the cards for printing
lines = [card.split("\n") for card in playerHand]
# Print one line at a time
for line in zip(*lines):
print(" ".join(line))
行
playerHand[i]=card[12::
修改了卡片列表。虽然你并不真正理解你的逻辑,但你一直在改变playerHand的值。您还可以在playerHand[0]时检查。一旦这是假的,你就退出。对同一方法的下一次调用将在while check上返回false。解决此问题的一种方法是在方法中使用\u playerHand=playerHand.copy()
复制列表。并在方法中使用_playerHand。您编辑了代码,但仍然是错误的。a=playHand.copy()