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

How was I supposed to know to use "self.name" when we didn't use this in the lessons before the challenge?

I'm having a hard time with the challenges in the learn python track for Object-Oriented Python. Not sure if it's because we are using the REPL instead of writing the code and transferring those examples to writing the script is not clear. For example, in the challenge after the lesson "Methods" we were supposed to do the following: "First, I need you to add a method named praise to the Student class. It should take the self argument. Then, inside the praise method, return a positive message about the student using the name attribute.

As an example, it could say "You're doing a great job, Jacinta!" or "I really like your hair today, Michael!".

Feel free to change the name attribute to your own name, too!"

After some struggling, I got to:

class Student:
    name = "Kylie"

    def praise(self):
        print("You're doing a great job, {}!".format(self))

I used just "self" and "print" because that is what we had done in the examples during the course and just 'name' did not work. After looking through the community I found we need to do:

class Student:
    name = "Kylie"

    def praise(self):
        return("You're doing a great job, {}!".format(self.name))

The course example that I was following was:

import random

class Thief:
    sneaky = True

    def pickpocket(self):
        print("Called by {}".format(self))
        return bool(random.randint(0, 1))

I'm trying to understand where it was explained how to do this in the course and if it wasn't why was I expected to know?

1 Answer

The answer appears to be in the next lesson "Method Arguments" - would suggest moving code challenge to after this lesson

Kylie! I just stumbled upon this too. I was going in circles too. I agree with your recommendation.