Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGilang Ilhami
12,045 PointsDRY 'Goblin' object has no attribute 'attack'
It works for character, but when i tried to run the goblin.attack()
AttributeError: 'Goblin' object has no attribute 'attack'
monster.py
import random
from combat import Combat
COLORS = ['yellow', 'red', 'blue', 'green']
class Monster(Combat):
min_hit_points = 1
max_hit_points = 1
min_experience = 1
max_experience = 1
weapons = 'axe'
sound = 'rawr'
def __init__(self, **kwargs):
self.hit_points = random.randint(self.min_hit_points, self.max_hit_points)
self.experience = random.randint(self.min_experience, self.max_experience)
self.color = random.choice(COLORS)
for key, value in kwargs.items():
setattr(self, key, value)
def __str__(self):
return '{} {}, HP: {}, XP: {}'.format(self.color.title(),
self.__class__.__name__,
self.hit_points,
self.experience)
def battlecry(self):
return self.sound.upper()
class Goblin(Monster):
max_hit_points = 3
max_experience = 2
sound = 'squeak'
class Troll(Monster):
min_hit_points = 3
max_hit_points = 5
min_experience = 2
max_experience = 6
sound = 'growl'
class Dragon(Monster):
min_hit_points = 5
max_hit_points = 10
min_experience = 6
max_experience = 10
sound = 'raaaaaaaaar'
combat.py
import random
class Combat:
dodge_limit = 6
attack_limit = 6
def dodge(self):
roll = random.randint(1, self.dodge_limit)
return roll > 4
def attack(self):
roll = random.randint(1, self.attack_limit)
return roll > 4
3 Answers
Ryan S
27,276 PointsHi Gilang,
I don't see anything wrong with your code, and if it works for the Character class then your Combat class should be fine.
Did you remember to re-import your Goblin class after adding the Combat extension? Simply saving the file isn't enough to update it when working in the shell.
If that isn't your issue, how did you initialize it? I'm guessing you ran:
>>> goblin = Goblin()
>>> goblin.attack()
Ahmad Lala
2,312 PointsDid you create an instance of your class before calling it? Time: 4.40 of the video.
Devon Kaahanui
Courses Plus Student 2,164 PointsI am getting the same problem, Kenneth please help!
Devon Kaahanui
Courses Plus Student 2,164 PointsActually successfully debugged, it was a directory thang. Best of luck Ryan!