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

Gilang Ilhami
Gilang Ilhami
12,045 Points

Using format

I think i forgot something about .format() Anybody can help?

method.py
class Store:
    open = 8
    close = 18
    hours = "We're open from {} to {}.".format(open, close)

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,031 Points

You need to add the method hours and not just create hours attribute.

Try starting with:

class Store:
    open = 8
    close = 18
    def hours(self):
        # TODO: define formatted string
        # TODO: return string
        [your code here]
Gilang Ilhami
Gilang Ilhami
12,045 Points

Hey Chris, thanks for the tip

but i think i still don't understand, something like this?

class Store:
    open = 9
    close = 18

    def hours(self):
        hours = "We're open from {} to {}.".format(open, close)
        return self.hours
Chris Freeman
Chris Freeman
Treehouse Moderator 68,031 Points

That's extremely close. Remember, when a method is run, it is operating on an instance of the class. In order to access attributes of the instance they must be prefixed with the instance reference "self." So, within the method the attributes would be referred to as self.open and self.close.

Gilang Ilhami
Gilang Ilhami
12,045 Points

Hey Chris, again thank you for your reply, But i still don't get the concept about it Is it suppose to be like this?

class Store:
    open = 9
    close = 18

    def hours(self):
        houres = "We're open from {} to {}".format(self.open, self.close)
        return self.hours
Chris Freeman
Chris Freeman
Treehouse Moderator 68,031 Points

You're almost there. There is a typo in "houres". Also the local variable hours doesn't need the "self." since it isn't an attribute.

Gilang Ilhami
Gilang Ilhami
12,045 Points

Thank you very much Chris :)