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

James N
James N
17,864 Points

something's is wrong with the Create a class with a method CodeChallenge

when i type the code at the bottom (labelled method.py) i just get an error telling me to try again! i've copied and pasted this entire code into a workspace and fiddled around using the python interpreter and everything works fine!

workspaceOutput.py
Python 3.5.0 (default, Oct 28 2015, 02:02:37)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from CodeChallenge import Store
>>> Store.hours(1,2)"We're open from 1 to 2"
>>> Store.hours(Store.open,Store.close)
"We're open from 10 to 1000" 

#See? it works perfectly!

(in case there's any crucial difference between my workspace code and my codechallenge code, i've pasted my workspace code below)

workspaceCode.py
class Store:
  open = 10
  close = 1000
  def hours(open,close):
    placeholder = "We're open from {opentime} to {closetime}"
    return placeholder.format(opentime = open, closetime = close)

Hope you can help! Thanks anyway, James.

James N
James N
17,864 Points

whoops! i though the button that said "add my codechallenge code" would work! oh well... the code is basically the same as the workspace code. i'll remove that extra part.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The difference between the interactive run and the code for the challenge, is the interactive run supplies two arguments to hours(), where in the challenge, no arguments are expected to be used for the method.

The method accesses the values of open and close through the instance variables self.open and self.close. As a instance method, hours() needs the instance variable self as the first parameter:

class Store:
    open = 10
    close = 1000
    def hours(self):
        placeholder = "We're open from {opentime} to {closetime}"
        return placeholder.format(opentime = self.open, closetime = self.close)
James N
James N
17,864 Points

thanks! i was a little confused! after all, it says to: "Replace the first placeholder with the open time and the second with the close time."

also, i didn't even know what self did, let alone how to use it.

Thanks alot! i've completed the challenge!