Home > Software engineering >  My loop keeps repeating same value when I want it to change with each instance
My loop keeps repeating same value when I want it to change with each instance

Time:12-14

Recently I've been tasked with creating a sort of game, where you have two objects (e.g. airplane1, airplane2) which are a part of a class called "Airplanes"

The Airplanes class is supposed to have a method called "shoot" which is supposed to accept objects of the class as its parameters. The method takes away a random amount of hp from the enemy and takes away double the random amount of health from the shooter, this is supposed to happen until one of the airplanes has 0 hp. I have everything in order, except my while loop keeps printing the same amount of ammo and health throughout all the 10 instances and it can never get down to 0, because it always keeps the value of the first instance.

import random

class Airplane:
    def __init__(self, name, maxHp, maxAmmo,):
        self.name = name
        self.maxHp = maxHp
        self.hp = maxHp
        self.maxAmmo = maxAmmo
        self.ammo = maxAmmo

    def shoot(self, target):
        randomAmt = random.randrange(10, 25)
        damage = randomAmt
        target.hp = target.maxHp - damage
        self.ammo = self.maxAmmo - damage * 2
        counter = 1
        while counter <= 10:
            print(f"{counter}    {self.name} │ {self.maxAmmo}/{self.ammo} │ {self.maxHp}/{self.hp}  :  {target.name} │ {target.maxAmmo}/{target.ammo} │ {target.maxHp}/{target.hp}")
            counter =1    
            
            
plane1 = Airplane(input("Enter name: "), 100, 250)
plane2 = Airplane(input("Enter name: "), 100, 250)


print(plane1.shoot(plane2))

CodePudding user response:

You have called the shoot method once in your code. The loop in the shoot method only prints the same info every time because that's what the code is written to do. Your loop does not retrigger shooting. Two options:

  • Move the while loop outside the class to call the shoot method multiple times.
  • Alternatively, after the print statement in the loop, call the self.shoot to retrigger shooting.

CodePudding user response:

take this sample but, add a STOP condition when self.hp get to 0

import random

counter = 1

class Airplane:
    def __init__(self, name, maxHp, maxAmmo,):
        self.name = name
        self.maxHp = maxHp
        self.hp = maxHp
        self.maxAmmo = maxAmmo
        self.ammo = maxAmmo

    def shoot(self, target):
        randomAmt = random.randrange(10, 25)
        damage = randomAmt
        target.hp = target.hp - damage
        self.ammo = self.ammo - damage * 2
        print(f"{counter}    {self.name} │ {self.maxAmmo}/{self.ammo} │ {self.maxHp}/{self.hp}  :  {target.name} │ {target.maxAmmo}/{target.ammo} │ {target.maxHp}/{target.hp}")


plane1 = Airplane(input("Enter name: "), 100, 250)
plane2 = Airplane(input("Enter name: "), 100, 250)


while counter <= 10:
    plane1.shoot(plane2)
    counter =1
  • Related