how do i do this one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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

17th Mar 2021, 3:37 PM
☯【 x e n o z 】☯
☯【 x e n o z 】☯ - avatar
2 Answers
+ 2
And what you need to do here? And can you please mention language name in tags.
17th Mar 2021, 3:47 PM
Abhay
Abhay - avatar
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.
17th Mar 2021, 4:15 PM
Ethan
Ethan - avatar