I am trying to remove classes from a list based on their hp. I am making a large scale battle simulator for a D&D campaign. Its a simple program that makes two lists of classes and pits them against each other.
I am running into a problem when it comes to removing dead fighters. It works fine if one fighter dies in a round, but when multiple die, it goes wonky.
def check_human_deaths():
for i in range(len(goodguys)):
if goodguys[i].hp <= 0:
print('{} has died...'.format(goodguys[i].name))
goodguys.remove(goodguys[i])
Removing the dead fighter changes the length of the list, throwing an index error:
IndexError: list index out of range
I am not to sure how to proceed with removing the dead from the battlefield. Any tips are appreciated. Let me know if I am going about this in a fundamentally wrong way.