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 trialChris Jones
Java Web Development Techdegree Graduate 23,933 PointsWhy isn't this code the correct answer?
Why isn't this code the correct answer to the quiz on Formatting Strings? I know there's a more straight forward way than this (subject = "Treehouse loves {}".format(name))), but I just wanted to try other ways of making this work. I put the code below into a workspace and it worked just fine. Are the quizzes just looking for pre-determined code and not necessarily evaluating the code for the correct string?
name = "Chris Jones" subject = "Treehouse loves {}" subject = subject.format(name)
2 Answers
Alexander Davison
65,469 PointsYou are doing a great job! The only problem is that you have to call "format" directly onto the string, not onto the variable. Instead of doing two lines like this:
name = "Chris Jones"
subject = "Treehouse loves {}"
subject = subject.format(name)
Where you called format on the subject variable, you have to it directly like this:
name = "Chris Jones"
subject = "Treehouse loves {}".format(name)
Hope that helps!
anil rahman
7,786 PointsI don't know python but this worked?
name = "Chris Jones"
subject = "Treehouse loves {}"
subject = "Treehouse loves {}".format(name)
Alexander Davison
65,469 PointsThat won't work. I tried it in the code challenge. If you get rid of the
subject = "Treehouse loves {}"
Line then it will work.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsThanks, Alexander! Why can't I use the .format() method on the variable? The variable is a string object, correct? From looking at the format method, it looks like the .format() method just needs a string as the object to manipulate, right? Maybe I'm thinking about that wrong. Here's what I found from typing
help(str.format):
Thanks for your help!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYes, you could do format on the variable, but code challenges are picky. The code challenge expects you to do it on a string.
Hope that makes sense!
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsAh, yes, that makes sense. I was wondering if the code challenges' correct answers are specific code or if it evaluates the code. I guess they're just looking for specific code.
Thanks for your help, Alexander:)!