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

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
229,744 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.