birkenspanner
birkenspanner

Reputation: 37

How to make a loop with functions outside a class

I'm kinda new to programming and I'm stuck at this problem:

I want to make a loop of functions outside their parent class, until the life value reaches 0, then I want the program to end.

class Enemy():

    def __init__(self, name, life):
        self.name = name
        self.life = life

    def attack(self):
        x = input("write 'attack' to attack\n")
        if x == 'attack':
            self.life -= 5

    def checklife(self):
        if self.life <= 0:
            print("Dead")
        else:
            print(self.name, "has", self.life, "life left")
        return  self.life

class Attack(Enemy):

    def loop(self):
        while self.life > 0:
            continue


enemy1 = Attack("Peter", 10)

# This are the functions I want to loop until self.life is 0
enemy1.attack()
enemy1.checklife()

Upvotes: 3

Views: 724

Answers (1)

sawreals
sawreals

Reputation: 338

Use a while loop in your main function. In order to call those two functions you have defined until the self.life is 0 a while loop would work as it checks the condition until it is true unlike an if statement which will only check once. I am assuming you also are defining an int value for life.

Try this in your main function:

while self.life > 0:
    enemy1.attack()
    enemy1.checklife()

Upvotes: 4

Related Questions