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

Jeremy Fisk
Jeremy Fisk
7,768 Points

Cannot figure out why i am getting an error asking if i defined my method?

Please assist in this challenge if possible, I have no clue why it wont work:

My code looks like this:

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

  def hours(self):
    return "We're open from " + self.str(open) + "to " + self.str(close)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

when it asks if something is define, that can mean that there is a syntax error in the code. In this case it is use of str. str() is a built-in function and is call directly: str(self.open). Other things to fix:

  • add space before "to"
  • add missing "." at end of string
class Store:
  open = 6
  close = 9

  def hours(self):
    return "We're open from " + str(self.open) + " to " + str(self.close) + "."

However, the challenge also asked to use the .format() method. Can you translate the above solution into one using .format()?

Jeremy Fisk
Jeremy Fisk
7,768 Points

Thank you so much for your time, I attempted to re-vamp and my new code looks like this:

class Store: open = 6 close = 9

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

but now when I execute the following instructions on my test.py

pennys=Store() pennys.hours()

my error says : NameError: name 'close' is not defined .

any insight ? most respectfully,

Jeremy

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

"close" is looked for in the following places:

  • a local variable to hours(): Nope!
  • a parameter to `hours()': Nope!
  • a instance attribute: Nope! (you'll learn more about these later)
  • a class attribute: BINGO! open and close are class attributes that can be reference using self.open and self.close.

Add self. to our format parameters.

BTW, the reason that open wasn't flagged as "not defined" is because open() is a built-in function which I suspect is not what you want to pass to your format() string. It's a HORRIBLE name to use as a coding example. IMHO

Jeremy Fisk
Jeremy Fisk
7,768 Points

Sir,

Thank you very much for your selfless use of time. I appreciate you taking the time to give me such a detailed and instructional response. It worked like a charm and I will be on to further challenges! All the best,