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

jose rodriguez
jose rodriguez
16,524 Points

what am I doing wrong with this Class Method? the prompt asks if I even defined it.

I am not understanding why it would ask me if I have even defined the Method. I do not see any other way of writing this code to make it work.

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



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

1 Answer

andren
andren
28,558 Points

The error is a bit misleading but there is in fact an issue in your code. The task is looking for an hours method within the Store class. And that does in fact not exist in your solution.

Remember that in Python indentation (horizontal spacing) is used to group code, since you have the method at the same indentation level as the class Python does not consider the method to be a member of the class. If you indent the method properly like this:

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

Then your code will work.

jose rodriguez
jose rodriguez
16,524 Points

Thank you andren! that worked, I reviewed the previous videos and it was done the original way i did. well now I know. thanks again!

andren
andren
28,558 Points

In the original videos they have more vertical (top to bottom) space , which is fine. The problem is the horizontal (left to right) space.

The method has to be indented to be one level within the class, that was done in the video preceding this challenge but not in your original answer. In my answer I changed the vertical spacing as well but that was mostly because I felt it looked better. Not changing the vertical spacing from your original answer but fixing the horizontal spacing like this:

class Store:
    close = 9
    open = 2



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

Would also work.