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

Andy Duncan
Andy Duncan
8,292 Points

Object-Oriented Python: Coding Store Class

I keep failing a challenge:

Here's the question:

Challenge Task 2 of 2

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's my solution:

class Store: open = 9 close = 5

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

What's wrong with this? I can't figure it out.

class Store:
  open = 9
  close = 5

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

2 Answers

Andy Duncan
Andy Duncan
8,292 Points

Oh, got it! :-)

class Store: open = 9 close = 5

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

Matt Nickele
Matt Nickele
468 Points

you should define a

Init(open,close): #function

with in this function create

  #self.open = open

also create

  #self.close = close  

have this function

 #return self 

then you create


In this function you should have if else statements to figure out if the store is open or close

finally create


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

current there is no point in having the class but the redesign will allow you to input a range of numbers and test if the store is open. It should also be a goal not to print in the class but rather leave printing for what is called the main portion of your program. This is why we use the str method it will automatically be called to print the object you created.

The init creates an object assigned to your class, it is what gives classes meaning, otherwise this is just a very complicated what to print a quick sentence

The functions init and str need the 2 underscore lines before and after the name I cant get box to leave them