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

Ahmed Elsawey
PLUS
Ahmed Elsawey
Courses Plus Student 3,527 Points

What is wrong with the last line or formatting the string?

Why s my formatting wrong? chris freeman

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I suspect the error message is 'close' is not defined. The question is then, for your "close", where is it defined?

"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 your format parameters: ...format(self.open, self.close)

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