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 trial

Python Object-Oriented Python (retired) Inheritance __str__

Evan Waterfield
Evan Waterfield
3,240 Points

Indention error on line 23 but I don't understand why it's an error

import random

COLORS = ['yellow', 'red', 'black', 'green']


class Monster:
    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 = '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 = 'raaaaaaaaaar'
Evan Waterfield
Evan Waterfield
3,240 Points

odd the copy and paste version of this has it unintended which would be an error. My work space has def str directly under def_init_

Evan Waterfield
Evan Waterfield
3,240 Points

works in my terminal but not in workspaces.

1 Answer

on line 7-12 and 14 you are two tabs in

on line 23 def, you are one tab in