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

Brent Liang
PLUS
Brent Liang
Courses Plus Student 2,944 Points

Bummer

The question specifies that keywords should be passed to .format() if the placeholders have names, what does it mean and what's wrong with the following code?

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

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

1) Don't use built-in functions name like that open() as a variable name. 2) All methods in class must have 'self' as the first argument. 3) Best way to init variables in class, use special method init. This method runs when you create instances of class every time.

Your code may be like that.

class Store:
    def __init__(self):
        self.start = 9
        self.end = 16

    def hours(self):
        return "We're open from {} to {}.".format(self.start, self.end)

c = Store()
print c.hours()
Steven Parker
Steven Parker
229,732 Points

:point_right: This challenge specifically asks you to use open as a variable name.

Brent Liang
Brent Liang
Courses Plus Student 2,944 Points

Thank you evgenypanov!!

I love your code and it definitely shows how important the "self" parameter is.

Sorry I have to give the best answer to Steven, would've chosen yours!! :)

1 Answer

Steven Parker
Steven Parker
229,732 Points

:point_right: You forgot to include "self" as the method parameter.

You'll also need to use it as a prefix for the class variables in the format.

You might want to review the video on Class Methods.