Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,167 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