Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's method

You are creating a shooting game! The game has two types of enemies, aliens and monsters. You shoot the aliens using your laser, and monsters using your gun. Each hit decreases the lives of the enemies by 1. The given code declares a generic Enemy class, as well as the Alien and Monster classes, with their corresponding lives count. It also defines the hit() method for the Enemy class. You need to do the following to complete the program: 1. Inherit the Alien and Monster classes from the Enemy class. 2. Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's hit() method. Sample Input: laser laser gun exit Sample Output: Alien has 4 lives Alien has 3 lives Monster has 2 lives. https://code.sololearn.com/c1pZb0jbnV31/?ref=app

2nd Jun 2021, 6:42 PM
Akash Sharma
Akash Sharma - avatar
1 Answer
0
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(Enemy): def __init__(self): super().__init__('Monster', 3) class Alien(Enemy): def __init__(self): super().__init__('Alien', 5) m = Monster() a = Alien() while True: x = input() if x == 'exit': break elif x == 'laser': a.hit() elif x == 'gun': m.hit()
2nd Jun 2021, 6:53 PM
Akash Sharma
Akash Sharma - avatar