+ 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?
9 Respostas
+ 12
line 38 and 40: you access instance, not call function, there's no needs of parenthesis after instance:
a.hit()
m.hit()
+ 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()
+ 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 :/
+ 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
+ 2
Does something like this help?
https://code.sololearn.com/cv64B28AfM61/?ref=app
+ 1
I think you don't need to put def hit Function on monster and alien class
0
Your while loop is okay but remove def hit on monster and alien, because there's super(), that's enough
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()
0
Shut up