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 Instant Objects Your first method

My code is working in my own environment, but not here. The error is about self, which I used correctly.

This is my code.

class Student: name = "Maryam"

def praise(self, st):
    if st in self.name:
        msg = "You're doing a great job, Maryam!"
        return msg
    return False

What is wrong with it?

first_class.py
class Student:
    name = "Maryam"

    def praise(self, st):
        if st in self.name:
            msg = "You're doing a great job, Maryam!"
            return msg

1 Answer

Josh Keenan
Josh Keenan
19,437 Points

Well it is because you are going about it in a way they don't want you to. When coding it is a good idea to remember the acronym KISS, Keep It Simple Stupid. When learning new things you can often over complicate them when the best solution is the simpler one.

class Student:
    name = "Your Name"

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

All you have to do is return a string saying something encouraging and including the name, since we are using a method we can't access name without using self, since it belongs to the class. You can't hardcode your name in their, the treehouse interpreter is looking to see you passing in the self.name to the return statement somewhere, it can't tell what is or isn't a name, it only sees a string and that you aren't passing it what you need to, self.name