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

Moises Miguel
Moises Miguel
5,028 Points

Now, add a method named hours that returns "We're open from {} to {}.". Replace the first placeholder with the open time

Now, add a method named hours that returns "We're open from {} to {}.". Replace the first placeholder with the open time and the second with the close time. Remember you need to pass keywords to .format() if your placeholders have names.

Is the 'self' in my code messing it up, the previous video said that it always has to have 'self' when making a function for a class.

method.py
class Store:
    open = 5
    close = 10

    def hours();
        return ("We're open from {} to {}".format(open, close))

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You were on the right track with using self.open and self.close but changed it. When defining the method you must have self as a parameter in the method definition. Also, you have syntax error. When you define the method, you have a semicolon at the end of the def instead of a colon. You have written def hours(); but that should be def hours(self):. Note the semicolon in your version and the colon in mine. After this, go back to accessing the open and close member variables by referencing self.

Also, your string is missing a full stop/period at the end after the last placeholder.

I hope this helps, but let me know if you're still stuck! :sparkles:

Moises Miguel
Moises Miguel
5,028 Points

Thank you for explaining it so clearly, fixed it on the first try!