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 trialJeremy Fisk
7,768 PointsCannot 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:
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
Treehouse Moderator 68,454 Pointswhen 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
7,768 PointsSir,
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,
Jeremy Fisk
7,768 PointsJeremy Fisk
7,768 PointsThank 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
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 Points"close" is looked for in the following places:
hours()
: Nope!open
andclose
are class attributes that can be reference usingself.open
andself.close
.Add
self.
to our format parameters.BTW, the reason that
open
wasn't flagged as "not defined" is becauseopen()
is a built-in function which I suspect is not what you want to pass to yourformat()
string. It's a HORRIBLE name to use as a coding example. IMHO