我正在尝试制作一张包含30张卡的纸牌,一次取出一张,然后使用pop将它们分成两个玩家,但我不知道为什么pop不起作用,为什么我的pop不起作用
我尝试使用.remove
,但我的老师说必须使用pop,但我不知道为什么pop不起作用
card_deck = [["yellow",1],["yellow",2],["yellow",3],["yellow",4], ["yellow",5],["yellow",6],["yellow",7],["yellow",8],["yellow",9], ["yellow",10],
["red",1],["red",2],["red",3],["red",4],["red",5],["red",6],["red",7], ["red",8],["red",9],["red",10],
["black",1],["black",2],["black",3],["black",4],["black",5],["black",6], ["black",7],["black",8],["black",9],["black",10]]
cardies = 30
while True:
if cardies > 0:
u1_card = random.choice(card_deck)
u2_card = random.choice(card_deck)
print ("player 1's card is", u1_card)
print("player 2's card is", u2_card)
card_deck.pop(u1_card)
card_deck.pop(u2_card)
len(card_deck) - 1
if u1_card == u2_card:
card_deck.pop
我希望纸牌(card_deck
)被随机洗牌,然后一次将一张纸牌分配给每个玩家(u1_card
,u2_card
),直到没有纸牌为止左。
答案 0 :(得分:0)
您要调用该函数,因此必须使用方括号。应该是:
card = card_deck.pop() # for your last line of code
此外,pop()
需要一个索引参数,因此card_deck.pop(u1_card)
将不起作用。您可以使用.index()
来获取索引,
card_deck.pop(card_deck.index(u1_card))
另外,我认为您的意思是用以下行更新cardies
:len(card_deck) - 1
cardies -= 1 # you might have to change this to 2, to get the desired result because you are popping 2 cards per loop iteration
您还应该有一个条件来结束while循环,否则当列表为空时会遇到错误,例如:
while len(card_deck) != 0:
...