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 trialMUZ140920 Kudakwashe Murungu
6,316 PointsMy code won't pass. After following up through - my player is not having the attribute attack()
Here are the classes
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
weapon = 'sword'
sound = 'roar'
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 = 'squeek'
class Troll(Monster):
min_hit_points = 3
max_hit_points = 5
min_experience = 2
max_experience = 6
sound = 'roar'
class Dragon(Monster):
min_hit_points = 5
max_hit_points = 10
min_experience = 6
max_experience = 10
sound = 'raaaaaaaaaar'
class Character(Combat):
experience = 0
hit_points = 10
def get_weapon(self):
weapon_choice = input("Weapon ([S]word, [A]xe, [B]ow: ").lower()
if weapon_choice in "sab":
if weapon_choice == 's':
return 'sword'
elif weapon_choice == 'a':
return 'axe'
else:
return 'bow'
else:
return self.get_weapon()
def __init__(self, **kwargs):
self.name = input("Name: ")
self.weapon = self.get_weapon()
for key, value in kwargs.items():
setattr(self, key, value)
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
1 Answer
Martin Cornejo Saavedra
18,132 PointsYou forgot to import class Combat in Character class:
from combat import Combat #Don't forget this
class Character(Combat):