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

Shawndee Boyd
Shawndee Boyd
6,002 Points

I am working on this challenge 2 of 2 and it is asking to use .format to pass and my return isnt working

class Store: open = 9 close = 9

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

5 Answers

Chris Jones
STAFF
Chris Jones
Treehouse Guest Teacher

You are really close but in your last answer to this question, you are trying to return hours(...). hours, in this scope is not a defined variable. hours is the name of your method on your Store class. Once this method has been called/invoked, you don't need to return hours(). You're already in the method, just build your formatted string and return it.

class Store:
    open = 9
    close = 9

Starting out very simply, we create a Store class with two properties for open and close. This part you have correct.

class Store:
    open = 9
    close = 9

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

The hours method simply needs to return a formatted string making sure to reference open/close properties from self.

I believe that you are misunderstanding how the hours method will be called, so maybe an example of the Store class in use would make more sense.

store = Store()

Here you create an instance of your Store object named store. From this store instance, you want to print out the store hours somewhere in your code. To do this, you simply call the hours method on your store instance.

print(store.hours())
>>> "We're open from 9 to 9."
David Bouchare
David Bouchare
9,224 Points

I think you're not too far off...

There is a problem with the:

return self.hours = ...

Also you're not using the properties of the current object with: format(open, close). It should be:

format(self.open, self.close)
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You can't really return an assignment, so just return the .format()-ed string.

And, like David Bouchare pointed out, you'll need to use self on those instance attributes.

Shawndee Boyd
Shawndee Boyd
6,002 Points

This is befuddling.

class Store: open = 9 close = 9

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

For the life of me, I can't understand why this code isn't working!

Shawndee Boyd
Shawndee Boyd
6,002 Points

I figured it out!!!!!!!!!!! Thank all of you guys for all the help!