Home > Enterprise >  Can't remove element from list in for loop python
Can't remove element from list in for loop python

Time:12-28

I'm making a card game in pygame and I'm storing the cards used in a list that receives the elements from a for loop which iterates through the files stores. I want to prevent the same card from being drawn twice so I thought that if the card has been drawn, remove it as an option from the list to prevent it. But the list removes method gives me an error saying that the element is not in the cards_list, yet it is.

cards_list = []
cards_tpye_1 = ['D', 'H', 'C', 'S']
cards_tpye_2 = ['Q', 'K', 'A', 'J']

for card in cards_tpye_1:
    for x in range(2, 11):
        img = pygame.image.load(f'img\cards\{card}{x}.png')
        img = pygame.transform.scale(img, (img.get_width() * 2, img.get_height() * 2))
        cards_list.append(img)

for card in cards_tpye_2:
    for suit in cards_tpye_1:
        img = pygame.image.load(f'img\cards\{card}{suit}.png')
        img = pygame.transform.scale(img, (img.get_width() * 2, img.get_height() * 2))
        cards_list.append(img)

dealer_card_1 = random.choice(cards_list)
card_1 = random.choice(cards_list)
card_2 = random.choice(cards_list)

cards_dealt = False
dealer_cards_list = []
player_cards_list = []

if cards_dealt is True:
       screen.blit(dealer_card_1, (width//3, 50))
       dealer_cards_list.append(dealer_card_1)
       screen.blit(card_1, (width//3, height//2))
       screen.blit(card_2, (width//3   140, height//2))
       player_cards_list.append(card_1)
       player_cards_list.append(card_2)
       cards_list.remove(card_1)
       cards_list.remove(card_2)
       cards_list.remove(dealer_card_1)

ValueError: list.remove(x): x not in list

I tried creating a new list of the dealt cards but it gives the same error when I attempt to remove the cards.

CodePudding user response:

random.choice has no knowledge of previously selected items so it is possible for your code to select the same card more than once. When you get to removing the cards, the second removal of a duplicate selection will fail.

You can get 3 different cards in one operation by using sample for 3 instead of calling choice 3 times:

dealer_card_1, card_1, card_2 = random.sample(cards_list,3)

This will ensure that the 3 cards are different and the rest of your code can stay as it is.

Another option could be to write a function that draws a card and removes it from the deck immediately. You could call this function anywhere (instead of random.choice) without spreading the removals all over the place:

def drawCard(deck):
    card = random.choice(deck)
    deck.remove(card)
    return card

...

dealer_card_1 = drawCard(cards_list)
card_1 = drawCard(cards_list)
card_2 = drawCard(cards_list)

...
# no need for the cards_list.remove() later on ...

CodePudding user response:

To prevent the same card from being drawn twice you could remove the card from the list right after it is drawn.

dealer_card_1 = random.choice(cards_list)
cards_list.remove(dealer_card_1)
card_1 = random.choice(cards_list)
cards_list.remove(card_1)
card_2 = random.choice(cards_list)
cards_list.remove(card_2)
  • Related