- 1
how do i do this one?
class Enemy: name = "" lives = 0 def __init__(self, name, lives): self.name = name self.lives = lives def hit(self): self.lives -= 1 if self.lives <= 0: print(self.name + ' killed') else: print(self.name + ' has '+ str(self.lives) + ' lives') class Monster: def __init__(self): super().__init__('Monster', 3) class Alien: def __init__(self): super().__init__('Alien', 5) m = Monster() a = Alien() while True: x = input() if x == 'exit': break
2 ответов
+ 2
And what you need to do here?
And can you please mention language name in tags.
0
so of this is the one I did you need to make monster and alien a subclass of enemy.
it can be done like this:
class Monster(Enemy):
then inside the while loop add to the if/else statement to call the hit method for Monster if the input is “gun” and call the hit method for Alien if the input is “laser”.
for example:
if the input is “gun” then it should execute m.hit() since you created a Monster object called m.
if this explaination is confusing try revisiting the inheritance lesson. in the intermediate python course.