0

I have a for loop in a section of code I have been working on where for every item in a list it loops a process of eventually removing one item and goes back around again.

When I run the code it stops at 10 items left in the list out of 30 items at the start.

cards_blue = ["b1","b2","b3","b4","b5","b6","b7","b8","b9","b10"]
cards_red = ["r1","r2","r3","r4","r5","r6","r7","r8","r9","r10"]
cards_yellow = ["y1","y2","y3","y4","y5","y6","y7","y8","y9","y10"]
deck = cards_blue + cards_red + cards_yellow
random.shuffle(deck)
for card in deck:

Then I have at the end of the card game process:

    if not deck:
        if card_score1 > card_score2:
            print("Player 1 is the final winner, they have the most cards")
        elif car_score2 > card_score1:
            print("Player 2 is the final winner, they have the most cards")
        print("Deck is empty")

At the end of all 30 loops of the game it will decide the winner using a scoring system but it stops before it can finish. I am unsure about why this is, could anyone help explain why this happens?

James
  • 32,991
  • 4
  • 47
  • 70
ben.m04
  • 41
  • 1
  • 9

1 Answers1

1

You should consider switching to a while loop if you will edit the list, for example:

while deck:
    card = deck.pop(0)
    ...
qmeeus
  • 2,341
  • 2
  • 12
  • 21