【问题标题】:Python card printerPython卡片打印机
【发布时间】:2020-05-31 18:04:58
【问题描述】:

我不明白为什么牌没有被第二次打印出来,即使字符串“Player's Hand”再次被打印出来。有人可以帮我找到再次打印它们的解决方案吗? 抱歉之前的错误,我已经全部改正了。

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)

【问题讨论】:

  • playerHand[i] = card[12:] 行修改了卡片列表。
  • 不太了解你的逻辑,但你一直在改变 playerHand 的值。您还检查while playerHand[0]。一旦这是错误的,你就会退出。下一次调用相同的方法将在检查时返回 false。
  • 解决此问题的一种方法是在您的方法中使用_playerHand = playerHand.copy() 对列表进行复制。并在你的方法中使用 _playerHand。
  • 您编辑了您的代码,但它仍然是错误的。 a = playHand.copy()

标签: python python-3.x user-defined-functions ascii-art


【解决方案1】:

如果您将每张卡片分成几行并一次打印具有相同数字的行,您的问题可以以更优雅的方式解决:

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多