[英]How to use pop on 2d arrays?
I am trying to make a card deck of 30 cards and take out one at a time and divide them between two players by using pop but I don't know why pop isn't working, why isn't my pop working 我正在尝试制作一张30张牌的牌组,一次取出一张牌,然后用两个牌子将它们分开,但我不知道为什么pop不能正常工作,为什么不是我的流行音乐
I have tried to use .remove
but my teacher says pop must be used and I don't know why pop isn't working 我曾尝试使用
.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
I expect the card deck ( card_deck
) to be shuffled randomly and then to be divided one card at a time to each player ( u1_card
, u2_card
) until there are no cards left. 我希望卡片组(
card_deck
)随机洗牌,然后一次一张卡片分配给每个玩家( u1_card
, u2_card
),直到没有卡片为止。
You want to call the function so brackets are necessary. 您想调用该函数,因此必须使用括号。 It should be:
它应该是:
card = card_deck.pop() # for your last line of code
Also, pop()
requires an index argument, so card_deck.pop(u1_card)
will not work. 此外,
pop()
需要一个索引参数,因此card_deck.pop(u1_card)
将不起作用。 You can get the index with .index()
so 您可以使用
.index()
来获取索引
card_deck.pop(card_deck.index(u1_card))
Also I think you mean to be updating cardies
with this line: len(card_deck) - 1
另外我认为你的意思是用这一行更新
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
You should also have a condition to end the while loop otherwise you will encounter an error when the list is empty, something like: 您还应该有一个条件来结束while循环,否则当列表为空时您将遇到错误,例如:
while len(card_deck) != 0:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.