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

Class method code challenge: returning string not working

My code is shown below. I can create the class fine but the second step says to define a class method named hours that returns the string that you can see in the code. I'm pretty sure I did the format function correctly.

Sage Elliott
Sage Elliott
30,003 Points

I don't think your code made it in the post, could you provide it?

I tried with and without using self as an argument.

class Store:
  open = 10
  close = 12

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

2 Answers

Sage Elliott
Sage Elliott
30,003 Points

Your solution is very close! You just need to pass 'self' into your hours def. *update after your updated post: Looks like you were not calling self on the open and close hours.

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

Ah, thanks, I could not for the life of me figure it out. But I forgot that you have to call self on the variables also.

But shouldn't it work anyways if I don't call self on anything?

Sage Elliott
Sage Elliott
30,003 Points

The open and close variables are not global. so you you have to pass them into you hours method. If you had multiple stores with multiple open hours you'd want to pass in the self value for each store to make sure you have the right values for each store.