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 trialPeter Kerre
280 PointsOK, so now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the va
OK, so now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject again.
name = "Peter"
subject = "Treehouse " + "loves " + " {} "
print(subject.format(name))
When I test from a Python App on my phone , my answer code passes but in the treehouse class it is being failed
[MOD: edited code block -srh]
3 Answers
Steve Hunter
57,712 PointsHi Peter,
Try:
name = "Steve"
subject = "Treehouse loves {}".format(name)
This assigns a string to the name
variable. Then, we create a subject
variable that contains the "Treehouse loves {}" piece. After that we use the format
method to insert the contents of name
where the curly braces are within the subject
string.
Does that make sense?
Steve.
Bob Buethe
3,276 PointsThe Treehouse lesson is expecting your code to return "Treehouse loves Peter" but it's seeing "Treehouse loves Peter"
You've got a space after "loves " and two more spaces in " {} " so you're getting back too many spaces in "loves Peter ". That's not what the quiz program is expecting.
Steve Hunter
57,712 PointsI tried messing with that but couldn't get it past the compiler even with edited spaces. Can you?
Bob Buethe
3,276 PointsI went back to that exercise and tried this...
name = "Bob"
subject = "Treehouse " + "loves " + "{}"
subject = subject.format(name)
...and it was accepted. Although...
name = "Bob"
subject = "Treehouse loves " + name
...is much simpler, and was also accepted.
Steve Hunter
57,712 PointsInteresting! That definitely didn't work yesterday - very odd!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI'm not exactly sure why the premise behind your method doesn't work - but the multiple concatenations add nothing to the problem being solved. Also, you aren't assigning the final string back into
subject
- the problem is not asking for a printed output. Messing with your code gives:This code fails but the output from this looks fine (if you remove the trailing space(s)) so I suspect the test is looking for something too specific. The {} must need to be live, perhaps?
Simplifying further:
This also fails. But taking the next logical step and removing the unnecessary
subject = subject
gives you:That's where the challenge was wanting you to end up so, unsurprisingly, it works!!
Steve.