【发布时间】: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