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!

Patricio Bolognini
Patricio Bolognini
1,491 Points

can't find the solution for warrior.py

I can't get the script to work. It falls into an error every time I run it, despite I think it shouldn't. My code is attached.

warrior.py
from character import Character;
class Warrior(Character):
  pass

  def __init__(self):
    self.weapon = 'sword';

  def rage(self):
    self.attack_limit = 20;

  def __str__(self):
    cadena = "Warrior, <" + self.weapon + ">, <" + self.attack_limit +">";
    return cadena;

3 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

There's a easier way of concatenation when you're inserting multiple values.

If the value is predefined, you can insert them via the .format() function.

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

note how these values will be inserted at the locations of the '{}' or curly brackets as they're commonly referred to. You can utilize this method as long as the variables are defined and everything is in proper order.

Think about what you were doing by declaring 'candena ='. Does your prior method allow for all the characters to utilize the __str__(self) function? And which version of the function is more useful?

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The specific error your are getting caused by trying to concatenate an int with a string. Two choices:

1. Wrap the int in a str():

  def __str__(self):
    cadena = "Warrior, <" + self.weapon + ">, <" + str(self.attack_limit) +">";
    return cadena;

2. Use the format() method as suggested by Andrew.