-1

I am trying to remove objects from an array without it overshooting. When it leaves the for then it removes the cards that have died.

Error:

 File "e:/AMS1/M3/Proyecte-Cartas-IETI/main.py", line 86, in <module>
    mainmenu()
  File "e:/AMS1/M3/Proyecte-Cartas-IETI/main.py", line 84, in mainmenu
    switch(opcion)
  File "e:/AMS1/M3/Proyecte-Cartas-IETI/main.py", line 21, in switch
    func()
  File "e:/AMS1/M3/Proyecte-Cartas-IETI/main.py", line 25, in partidajvsj
    partida.partida(cartas, cartasE)
  File "e:\AMS1\M3\Proyecte-Cartas-IETI\partida.py", line 22, in __init__
    partida.attack(turn)
  File "e:\AMS1\M3\Proyecte-Cartas-IETI\partida.py", line 146, in attack
    mesaA.pop(muertes[i])
IndexError: pop index out of range

Code:

if(turn==1):
    for i in range(len(mesaE)):
        if i == len(mesaA):
            break
        if i >= len(mesaA):
            player2.health = player2.health - mesaA[i].attack 
        if mesaE[i].attack >= mesaA[i].defense:
            print("Info: Card Dead ", mesaE[i].name)
            checkKill = True
            muertes.append(i)
            if mesaE[i].attack>mesaA[i].defense:
                player1.health = player1.health - (mesaE[i].attack-mesaA[i].defense)
                print("Le quito: ", (mesaA[i].defense-mesaE[i].attack))
                print("The player loses health! Player 1: ", player1.health)
    if checkKill == True:
        for i in range(len(deaths)):
            if i == len(deaths):
                break
            print("Card Death: ", muertes[i])
            mesaA.pop(muertes[i])
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

You are trying to pop an element from the list “mesaA” but the index (which is meurtes[i]) you are trying to pop is not on that list Just make sure that the index you’re giving is in the list