I need help ill show the error and my code

Traceback (most recent call last):
File “main.py”, line 98, in
first_card = played_cards[0]
IndexError: list index out of range


Repl link:

played_cards.insert(0,player_1_cards[0])
player_1_cards.remove(player_1_cards[0])

first_card = played_cards[0]
1 Like

that happens when the player has no more cards and you should break out of the loop before that happens

i dont realy know what you are talking about if the loop ends the game ends.

1 Like

no, as you can see, that doesn’t happen. While loops check the conditional AFTER the body is completed, which is why you’re receiving an out of bounds error – it’s empty but it doesn’t register that until after an error is thrown

so should i put the

first_card = played_cards[0]

after each

elif(player_1_input == 2):
if(first_card in RED_CARDS and (player_1_cards[2] in RED_CARDS)):
played_cards.insert(0, player_1_cards[2])
player_1_cards.remove(player_1_cards[2])

  elif(first_card in BLUE_CARDS and (player_1_cards[2] in BLUE_CARDS)): 
    played_cards.insert(0, player_1_cards[2])
    player_1_cards.remove(player_1_cards[2])

  elif(first_card in GREEN_CARDS and (player_1_cards[2] in GREEN_CARDS)): 
    played_cards.insert(0, player_1_cards[2])
    player_1_cards.remove(player_1_cards[2])

  elif(first_card in YELLOW_CARDS and (player_1_cards[2] in YELLOW_CARDS)): 
    played_cards.insert(0, player_1_cards[2])
    player_1_cards.remove(player_1_cards[2])
1 Like

you should probably check if the player decks are empty and breaking if they are

im trying to make it take the last card in the played_cards list and put it in the first_card variable everything else im going to do later, one error at a time.

1 Like