Shooting game in python sololearn

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?

line 38 and 40: you access instance, not call function, there's no needs of parenthesis after instance: a.hit() m.hit()

Shooting game in python sololearn

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()

Shooting game in python sololearn

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 :/

Shooting game in python sololearn

Does something like this help? https://code.sololearn.com/cv64B28AfM61/?ref=app

Shooting game in python sololearn

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

Shooting game in python sololearn

I think you don't need to put def hit Function on monster and alien class

Shooting game in python sololearn

Your while loop is okay but remove def hit on monster and alien, because there's super(), that's enough

Shooting game in python sololearn

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()

Shooting game in python sololearn

Please i need help on this challenge i have been stuck on it for a while and i need help or a answer preferably a answer.

This worked for me :👇 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()

Shooting game in python sololearn

Shooting game in python sololearn

class Enemy: name = "" lives = 0 def __init__(self, name, lives): self.name = name self.lives = lives def shooting(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 shooting(self): super().shooting() class Alien(Enemy): def __init__(self): super().__init__('Alien', 5) def shooting(self): super().shooting() m = Monster() a = Alien() while True: x = input() if x == 'exit': break elif x == 'gun': m.shooting() elif x == 'laser': a.shooting()

Shooting game in python sololearn

This worked for me :👇 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()

Shooting game in python sololearn