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) Objects Create a Class with a Method

Why isn't my code working?

Very easy challenge, but my code isn't working.

method.py
class Store:
  open = 9
  close = 2

  def hours(self):
    my_string = "We're open from {} to {}.".format(open, close)
    return my_string

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

You need to prefix the properties of a class with self when referring to them in a method, e.g. self.open.

so instead of return my_string, should I put "return self.my_string"? That not working either.

Trevor Skelton
Trevor Skelton
1,101 Points

The properties open and close are properties of the class. They are properties each instance of the class Store has. Python methods in a class all have the self parameter. This parameter is used to refer to the instance of Store calling the method. We use self to access the properties of each instance. Your my_string is simply a variable that is made inside your method. Once the method finishes and returns, that variable is gone. We don't need to refer to it outside of the function.

Sherry Parker
Sherry Parker
9,601 Points

Thanks for the answer Dan. I was really struggling to figure this one out. I had thought since I had called self for the method i wouldn't need to call it out for each item also. Another lesson learned.