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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Class method is not passing

The method hours works in workspaces. If I try it in the challenge however, it gives the error "Try again". I know it something tiny that I cannot figure out which is causing this issue. Kindly assist me.

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

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

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

My object oriented Python is pretty rusty so I can't give a detailed explanation for this, but with a bit of trial and error I was able to get this to pass the challenge.

You only need to pass 'self' to the function, not open and close. Then you format the string with self.open and self.close, rather than just open and close.

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

I gave that a try and it did not work. See the code below:

     class Store:
        open = 9
        close = 12
        def hours(self):
            print ("We're open from {} to {}. ".format(self.open, self.close))
Stuart Wright
Stuart Wright
41,120 Points

In your more recent attempt you are printing when it asks you to return. I believe the rest is correct.