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

Method not working in challenge but seems to work fine in console.

Here is the challenge question:

Now, add a method named hours that returns "We're open from {} to {}.". Replace the first placeholder with the open time and the second with the close time. Remember you need to pass keywords to .format() if your placeholders have names.

Here is my code:

class Store:
    open = 9
    close = 10

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

I try it in a Python console and it gives me the expected output:

We're open from 9 to 10

But I can't get it to work in the challenge and it gives me no hints on how to fix it. Just a "Bummer! Try again!"

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Your hours method doesn't have self as the first (and, really, only) argument and your open and close variables, inside, should be self.open and self.close. Other than that, looks good.

Thank you for that, it does make sense now.

class Store:
  open = 9
  close = 10

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

That worked to complete the challenge, but I am sure I could just remove the variable and just return it like this?

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

mine failed to run initially due to indendation

mine failed to run initially due to indendation

thank you Kenneth the method worked