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

alex albas
seal-mask
.a{fill-rule:evenodd;}techdegree
alex albas
Front End Web Development Techdegree Student 2,190 Points

How implement this method?

I can't write correctly this method..where is the fail? Thanks!

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

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

1 Answer

Samuel Webb
Samuel Webb
25,370 Points

Firstly, you don't need to bring open and close into the function. The self argument is making those accessible. Then, you'll need to use placeholders between your curly braces. I used {opening} and {closing}. These are what you'll associate your values to once you use the format function. Inside of the format function, you want to associate the placeholders with the values, like: opening=self.open. In this instance, self is referring to the Store class and it's accessing the open property. Your code should look like this:

class Store:
  open = 9
  close = 9

  def hours(self):
    return "We're open from {opening} to {closing}.".format(opening=self.open,closing=self.close)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You don't have to use the placeholders.

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

will work just fine.

Samuel Webb
Samuel Webb
25,370 Points

Oh man. I was reading the question a bit incorrectly. I thought it was asking for placeholders but it was actually just a reminder that if you have placeholders, you need to associate them in the .format() function. DOH!