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

William Watlington
William Watlington
3,347 Points

Can't figure out how to use .format in a class method.

On this code challenge, it asks me to create a method inside of the class Store. I think I understand that from the video but can't seem to figure out how to use the .format in it. I feel lost for the first time so I would appreciate help!

Thanks in advance!

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

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You are very close. You need some "self help":

class Store:
    open = 9
    close = 7

    def hours(self):
        # refer to attributes using the 'self.' prefix
        openclose = "We're open from {} to {}.".format(self.open, self.close)
        # local variables do not use 'self.' prefix
        return openclose
William Watlington
William Watlington
3,347 Points

Ohh! For some reason that thought didn't even enter my mind. Thanks!