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 Intro to Inheritance

Where to place a series of if/elif statements that regulate the hp from the color of a monster?

During the video I was a little overexcited to get into a more detailed system of the randomness of the monster's hit_points. However when running the code, I'm not quite sure where to place it, or if this code snippet will even work with classes.

def hp_due_color(Monster):
  if monster.color == 'green':
    max_hit_points = 3
  elif monster.color == 'blue':
    max_hit_points = 4
    min_hit_points = 2
  elif monster.color == 'yellow':
    min_hit_points = 3
  elif monster.color == 'red':
    min_hit_points = 4

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The code as written will do much. max_hit_points and min_hit_points are local to the function and don't affect the monster instance pass to this function. Prefix each of these variables with "monster." as shown below:

def hp_due_color(monster):  #<-- lowercased to match usage inside function
    if monster.color == 'green':
        monster.max_hit_points = 3  #<-- add "monster."
    elif monster.color == 'blue':
        monster.max_hit_points = 4  #<-- add "monster."
        monster.min_hit_points = 2  #<-- add "monster."
    elif monster.color == 'yellow':
        monster.min_hit_points = 3  #<-- add "monster."
    elif monster.color == 'red':
        monster.min_hit_points = 4  #<-- add "monster."

Once those changes are made, you can use this function at anytime after a Monster() has been instantiated such as:

monster_1 = Monster()
hp_due_color(monster_1)

thanks, glad it wasn't too far off.