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 trialBill Jones
346 PointsWhy is my .format usage incorrect?
I am trying to learn how do use the .format with a string in Python. The current task instructs me to do the following:
"OK, now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject (so start with subject =). You do not need to print() anything."
I get the following error message:
" Bummer! Be sure to use the {}
placeholder and the .format()
method."
Yet, I am doing both. When I try this in the workspace, it works.
name = "William F. Jones"
subject = "Treehouse loves {}"
subject.format(name)
Jason Anders
Treehouse Moderator 145,860 PointsHi there, louis coleman,
The challenge explicitly says not to print anything. So, adding a print
will cause another error. The problem is how the format
method is being applied. Supplied answer is below.
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey Bill,
Challenges are very strict in how it checks your code, so instructions need to be followed exactly in order for the checker to pass you.
Here, the instructions specifically say to
use .format() on the string "Treehouse loves {}"
Note the key phrase "on the string" ... you are using it on the variable.
So, instead of two lines of code, the challenge is looking for the DRYer way of just one line.
subject = "Treehouse loves {}".format(name)
Hope that helps. :)
Keep Coding!
Bill Jones
346 PointsThank you. I actually considered that approach, but rejected it. While in the long, distant past I was proud of writing tight (DRY - new term for me) code, I didn't think combining the two lines in this case provided any advantage, and in fact would be problematic for the intended usage (email subject line generator). Admittedly, I've been out of technology for almost two decades and it's been three since I've moved code to prod - so I have a lot to learn. I'll look for those contextual clues on the challenges in the future.
louis coleman
1,753 Pointslouis coleman
1,753 PointsYour .format function looks fine, however you are missing a print function. Try print(subject.format(name))