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

scope with .format

I don't understand the scope of open and close. What am I doing wrong?

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

2 Answers

.format is a function which replaces every {} in a string a variable that you pass through the function. The variables must be passed through the function in the intended order within the string.

So here you are having an error because you forgot your ( and ) for the str function. The corrected code would be:

class Store:
    open = 6
    close = 8
    def hours():
        return str("We're open from {} to {}.".format(open, close))
Steven Parker
Steven Parker
217,469 Points

:point_right: You don't need "str", but you might need "self".

Your return statement is formatting a string, which will return a string, so you don't need the "str" function at all.

But you probably do want to have hours use "self" as an argument, and then use "self" to prefix the names of the attributes being passed to format.