import random
deckTypes = []
cardType = ["Spade", "Hearts", "Diamonds", "Clubs"]
cardValues = ["Ace", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
Values = [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
for i in cardType:
for j in cardValues:
deckTypes.append(str(j) + " of " + str(i))
deck = dict(zip(deckTypes, Values*4))
playerValue = []
playerHand = []
def drawPlayerCard():
card = random.choice(list(deck))
playerHand.append(card)
print(playerHand)
for p in playerHand:
playerValue.append(deck[p])
print(playerValue)
print("Your Cards are:", playerHand)
print("total value of:", sum(playerValue), "\n")
if len(playerHand) < 2:
drawPlayerCard()
else:
pass
drawPlayerCard()
但是当我这样做时,我得到第一个数字2次。我知道为什么这是因为代码循环通过for循环2次,但我怎么能改变这个,这样我得到第一个和第二个没有2次获得第一个。
结果:
Your Cards are: ['3 of Clubs']
total value of: 3
['3 of Clubs', 'J of Diamonds']
[3, 3]
[3, 3, 10]
Your Cards are: ['3 of Clubs', 'J of Diamonds']
total value of: 16
答案 0 :(得分:0)
以下是发生的事情:
代码选择一张卡并将其放在手中,我们得到:
Hand: X
Value:
然后它获得该卡的价值,所以现在我们有:
Hand: X
Value: X1
现在,再次调用该函数,因为我们只有不到两张卡,所以我们添加另一张卡。
Hand: X, Y
Value: X1
但请注意你如何得到你的卡值,你迭代手并获得价值。因此,如果我们用X的手来做,我们会得到:
X1, Y1
但是,我们已经列出了X1!那么,我们最终会得到什么呢?
Value: (X1 added by the first function call), (X1, Y1 added by the second function call)
你需要做些什么来解决这个问题(被授予,有许多可能的解决方案,这只是一个)是分离绘制卡的逻辑并计算其值。我建议创建第二个函数,它接收玩家手的输入并生成你的总和。