我是Python新手,我正在和我的一个朋友用Python编写二十一点游戏。我在这行收到 Keyerror:0:
card1=random.choice(cards)
card2=random.choice(cards)
是书写错误还是更大的问题?请帮助我
对于上下文,这是包含这些行的代码部分:
import random
nplayers=int(input(f"Insert the number of players: "))
pplayers=1
cards={
1: "A",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: ["10", "J", "Q", "K"],
11: "A"
}
card1=random.choice(cards)
card2=random.choice(cards)
def give_cards():
print(f"Player {pplayers} cards are: " + str(card1) +"; " + str(card2))
while pplayers<=nplayers:
give_cards()
pplayers+=1
card1=random.choice(cards)
card2=random.choice(cards)```
字典不是序列。 尝试获取键或值:
card1=random.choice(cards.keys())
或
card2=random.choice(cards.values())
然后你应该想想当它获得 10 值(列表)时你会做什么