banana
banana

Reputation: 31

how do I move something from one list to another

I am trying to make a card game, but I am having trouble moving items from players deck to discard pile.

I tried using pop() so that way when the player draws it removes it from the deck, but I couldn't figure out how to use pop() and the random function.

player1s_hand = []
for number in range(5): 

I tried using pop() on the next line but then the cards aren't random.

card = random.choice(player1s_deck)
card = player1s_deck.pop()
player1s_hand.append(card)
print(player1s_hand)
player1s_discard = player1s_hand 

I expected to be able to remove cards from deck randomly but when I use random it doesn't let me pop()

Upvotes: 1

Views: 51

Answers (1)

Alibi Yeslambek
Alibi Yeslambek

Reputation: 83

You can choose random idx of card from the deck, put it to the end of the deck. And then pop it.

deck_sz = len(deck)
card_idx = random.choice(range(deck_sz))
player_hand.append(deck[card_idx])
deck[card_idx], deck[card_sz - 1] = deck[card_sz - 1], deck[card_idx]
deck.pop()

Upvotes: 1

Related Questions