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) Hack-n-Slash Warriors! Come Out and Play-ay!

Warrior Challenge 4 of 4

Please give me a tip, what could be wrong with my code...?

I get: "Bummer Try Again!"

warrior.py
from character import Character


class Warrior(Character):
  weapon = 'sword'


  def rage(self):
    self.attack_limit = 20
    return "Warrior, {}, {}".format(weapon, self.attack_limit)
  pass

3 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Christine.

First thing, get rid of that "pass". We do not need it anymore.

We need to use

def __str__(self):

when we want to let the compiler know the way we want to convert the class into a string.

And I see is that you have put "Warrior" in the string, but we need to substitute it with a placeholder: "{}". Then, in the format, as the first value, we need to put the class name, so: self.class.name

Also, please change weapon to self.weapon as you forgot the "self" there.

This result into the finale code for this last task of:

def __str__(self):
  return "{}, {}, {}".format(self.__class__.__name__, self.weapon, self.attack_limit)

Please not that "def" should here be in the same column as the previously used def rage(self)

Let me know if it is at least a bit clearer now.

Vittorio

Mikael Enarsson
Mikael Enarsson
7,056 Points

You are returning the string from the rage method, but the task is for you to return the string from the method called when "Warrior" is converted to a string, which would be the "str" method.

def __str__(self):
   ...
   ...

Also, you are missing a self in the string format arguments.

Edit: Damn, too late XD

Jose Luis Lopez
Jose Luis Lopez
19,179 Points

from character import Character

class Warrior(Character): weapon = 'sword' attack_limit = 20

def rage(self): return Warrior

def str(self): return "{}, {}, {}".format(self.class.name, self.weapon, self.attack_limit)