Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Hack-n-Slash Warriors! Come Out and Play-ay!

SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

OOPS

Is there any thing wrong in my rage method declaration. ?.

warrior.py
from character import Character
class Warrior(Character):
  weapon = 'sword'

  def rage(self):
    attack_limit = 20
Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

While monitors are stating the indention OK (I hope meaning it is not the root cause of the issue), Please get in the habit of using 4-space indentation (and not a TAB).

  • Please see the Treehouse Challenge on Python Indentation in the Write Better Python course. You will learn that 4-spaces is the preferred style for readability.
  • Please read PEP 8 on Indentation which says: Use 4 spaces per indentation level. Period.
  • As you advance you find out about pylint. It checks coding style against common practices. On this code, you would get the warning: Bad indentation. Found 2 spaces, expected 4 (bad-indentation)

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

Dave Campbell , the indentation is okay here; in Python, as long as the indentation level and tab width is consistent, code will run.

and SUDHARSAN CHAKRAVARTHI the problem of your code is that

Make a new Warrior method called rage that sets attack_limit to 20.

attack_limit is an attribute in the class, you need to refer to it as self.attack_limit.

from character import Character

class Warrior(Character):
  weapon = 'sword'

  def rage(self):
    self.attack_limit = 20
Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

I would argue that while the indention is not syntactically wrong, it is hardly "OK". -1 for condoning 2-spaces.

  • Please take the Treehouse Challenge on Python Indentation in the Write Better Python course. You will learn that 4-spaces is the preferred style for readability.
  • Please read PEP 8 on Indentation which says: Use 4 spaces per indentation level. Period.
  • Please run pylint on this code. You get the warning: Bad indentation. Found 2 spaces, expected 4 (bad-indentation)
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Well I can only agree 4 is the standard for indentation, but all python code challenges here I think have 2 as indentation default.

I have always used that default (I think) for code challenges, while I try to stick to 4 in general and also workspaces.

For this specific case I said indentation was not a problem as it is NOT what is preventing him from passing the code challenge. I thought (and stil l think) that the other mistake really needed to be underlined instead.

;) Vittorio

William Li
William Li
Courses Plus Student 26,867 Points

Hi there, chris freeman , yeah I agree with you too on the 4 spaces indentation; and Vittorio Somaschini 's comment is pretty much everything I wanna say, there's only 1 thing I want to add.

The bigger problem is why the challenges have a default spacing of 2-spaces

In some newer Python courses, (database course, for example), the Code challenges are indented using 4 spaces, I'm not sure why the codes in older courses were still having 2 spaces indentation as default; maybe it's too time-consuming to go back and redo the indentation for every Code challenges? I don't know.

Dave Campbell
Dave Campbell
13,310 Points

If you wrote it here the same way it is in your code, you might need to correct the indentation.

Otherwise, double check to make sure you are providing a variable when you call the function.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

Why was this down graded without commend. Rude. While this answer didn't completely solve the issue, was it voted down because of the indention comment? +1 for indention support!

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Sudharsan.

I think your indentation is fine, as being a method I think is it ok.

The problem I see here is that you did not put the attack_limit=20 actually to that self, so it does not change the attack_limit.

You simply need to add self. right before attack_limit =20 resulting in

self.attack_method = 20

I hope I made it clear ;)

Vittorio

Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

You had different indention advice on this question. The challenge wants 4-spaces because it Treehouse is trying to teach a better style not just "OK".

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

That particular challenge asks for changing indentation...

Anyway, I agree with you Chris: 4 spaces indentation is surely better, but I thought it was not the main problem here.

It is really as simple as that.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,082 Points

I'm glad that you agree with the 4-space indent.

Perhaps the better wording would have been "The indentation is not causing the problem". With a comment that 4-space indent is the norm.

The bigger problem is why the challenges have a default spacing of 2-spaces. It reinforces bad habits. In Workspaces, you can change the default from 2- to 4-spaces, but not in the challenges.

SUDHARSAN CHAKRAVARTHI
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

Hello Vittorio Somaschini. Thank You very much. Yes. You are right. I understood.