Intermediate Python OOP Project "Shooting Game" Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Intermediate Python OOP Project "Shooting Game" Help

I'm working through the Intermediate Python course and I finished the OOP module, but I can't seem to figure out the "Shooting Game" project. The instructions say: "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." My code so far is here: https://code.sololearn.com/ca163a0a11a2 I keep getting a TypeError: "Traceback (most recent call last): File "/usercode/file0.py", line 40, in <module> m().hit() TypeError: 'Monster' object is not callable" Please help?

6th Feb 2021, 12:03 AM
Matt Chaplin
Matt Chaplin - avatar
9 Answers
+ 12
line 38 and 40: you access instance, not call function, there's no needs of parenthesis after instance: a.hit() m.hit()
6th Feb 2021, 12:44 AM
visph
visph - avatar
+ 32
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) def hit(self): super().hit() class Alien(Enemy): def __init__(self): super().__init__('Alien', 5) def hit(self): super().hit() m = Monster() a = Alien() while True: x = input() if x == 'exit': break elif x == 'laser': a.hit() elif x == 'gun': m.hit()
11th Mar 2021, 7:54 AM
Sunesh Johnson
Sunesh Johnson - avatar
+ 4
visph and Steven M, thank you very much! I hate it when such a little thing in my code messes the whole thing up and I can't tell why based on the error message :/
6th Feb 2021, 6:40 PM
Matt Chaplin
Matt Chaplin - avatar
+ 3
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=='laser': Enemy.hit(a) if x=='gun': Enemy.hit(m) if x == 'exit': break
11th Feb 2022, 9:29 AM
Alcino Castelo
+ 2
6th Feb 2021, 12:49 AM
Steven M
Steven M - avatar
+ 1
I think you don't need to put def hit Function on monster and alien class
15th Feb 2021, 7:14 AM
Ratna13
Ratna13 - avatar
0
Your while loop is okay but remove def hit on monster and alien, because there's super(), that's enough
15th Feb 2021, 7:15 AM
Ratna13
Ratna13 - avatar
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() else: m.hit()
19th Sep 2021, 6:29 PM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
0
Shut up
25th Aug 2023, 7:01 PM
Terry Hougan - 418572