While loop not as expected
This is my Code:
class Warrior: def __init__(self): self.name="Warrior" self.health=50 self.attack=5 self.is_alive=True class Knight: def __init__(self): self.name="Knight" self.health=50 self.attack=7 self.is_alive=True class Army: def __init__(self): self.units=[] def add_units(self, Unit_type, amount): for i in range(amount): self.units.append(Unit_type) class Battle: def fight(self, army1, army2): x=0 y=0 while x<len(army1.units) and y<len(army2.units): if singlefight(army1.units[x], army2.units[y]): y+=1 print("y:"+str(y)) print(str(army1.units[x].name)+" wins") else: x+=1 print("x:"+str(x)) print(str(army2.units[y].name)+" wins") def singlefight(unit_1, unit_2): round=0 while unit_1.is_alive and unit_2.is_alive: round+=1 print ("Round "+str(round)+"\n"+str(unit_1.name)+":"+str(unit_1.health)+" "+str(unit_2.name)+":"+str(unit_2.health)) attack(unit_1, unit_2) print(str(unit_1.name)+" attacks "+str(unit_2.name)+" and deals " +str(unit_1.attack)+" damage") print(str(unit_2.name)+" now has " +str(unit_2.health) + " health") if unit_1.is_alive and unit_2.is_alive: attack(unit_2, unit_1) print(str(unit_2.name)+" attacks "+str(unit_1.name)+" and deals " +str(unit_2.attack)+" damage") print(str(unit_1.name)+" now has " +str(unit_1.health) + " health") return unit_1.is_alive def attack(attacker, defender): defender.health-=attacker.attack if defender.health<=0: defender.is_alive=False if __name__ == '__main__': # chuck=Knight() # chip=Warrior() # fight(chuck, chip) army1=Army() army1.add_units(Knight(), 5) army2=Army() army2.add_units(Warrior(), 6) battle1=Battle() battle1.fight(army1, army2)
A lot of print commands for debugging. I use a while loop with iterations for the two army units. During the first loop the battle class function goes into my singlefight function. But any consecutive loops seem to not go there... Why?!? Output:
Round 1 Knight:50 Warrior:50 Knight attacks Warrior and deals 7 damage Warrior now has 43 health Warrior attacks Knight and deals 5 damage Knight now has 45 health Round 2 Knight:45 Warrior:43 Knight attacks Warrior and deals 7 damage Warrior now has 36 health Warrior attacks Knight and deals 5 damage Knight now has 40 health Round 3 Knight:40 Warrior:36 Knight attacks Warrior and deals 7 damage Warrior now has 29 health Warrior attacks Knight and deals 5 damage Knight now has 35 health Round 4 Knight:35 Warrior:29 Knight attacks Warrior and deals 7 damage Warrior now has 22 health Warrior attacks Knight and deals 5 damage Knight now has 30 health Round 5 Knight:30 Warrior:22 Knight attacks Warrior and deals 7 damage Warrior now has 15 health Warrior attacks Knight and deals 5 damage Knight now has 25 health Round 6 Knight:25 Warrior:15 Knight attacks Warrior and deals 7 damage Warrior now has 8 health Warrior attacks Knight and deals 5 damage Knight now has 20 health Round 7 Knight:20 Warrior:8 Knight attacks Warrior and deals 7 damage Warrior now has 1 health Warrior attacks Knight and deals 5 damage Knight now has 15 health Round 8 Knight:15 Warrior:1 Knight attacks Warrior and deals 7 damage Warrior now has -6 health y:1 Knight wins y:2 Knight wins y:3 Knight wins y:4 Knight wins y:5 Knight wins y:6 Knight wins