+ 1

Intermediate Python Shooting game(quiz before exceptions)

There are preset classes and the task is: 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. I add a simple else-if cycle as I read some solutions are the same else: if x == 'laser': a.hit() if x == 'gun': m.hit() but that did not work then tried else: if x == 'laser': a._hit() if x == 'gun': m._hit() or else: if x == 'laser': Enemy._a.__hit() if x == 'gun': Enemy._m.__hit()

5th Dec 2022, 12:55 PM
Yusuf Uveys Durudeniz
3 odpowiedzi
+ 6
kwaggen • > both new classes *Monster* and *Alien* are not inheriting properly from the *Enemy* class. it has to be like: class Monster(Enemy): def __init__(self): super().__init__('Monster', 3) > also the required loop at the end of the code is not complete. currently we only have implemented *exit*, but *laser* and *gun* are still missing.
5th Dec 2022, 7:58 PM
Lothar
Lothar - avatar
+ 2
I presume that else has a preceding if. Is there a hit method defined for the class?
5th Dec 2022, 1:19 PM
Lochard
Lochard - avatar
0
yes, here are classes and so 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
5th Dec 2022, 5:31 PM
Yusuf Uveys Durudeniz