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

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,454 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,454 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,454 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 :)