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 Python Basics (2015) Python Data Types String concatenation

Alphonso Sensley II
Alphonso Sensley II
8,514 Points

What is wrong with my python code? Python Basics

I know it's simple; but I cant figure out why my code works in workspaces but not for the challenge.

strings.py
name = "Alphonso"
subject = "Treehouse loves {}"
subject.format(name)

1 Answer

Mattias Nelson
Mattias Nelson
961 Points

Note: I'm still learning all this fancy stuff myself, just wanted to give ya the heads up!

I'm guessing that even if you use another method which was technically right, the challenge REALLY wants you to follow the directions/instructions down to the letter. E.G. ya need to assign "subject" with the string, variable, and + it's requesting before it'll flag you as completed.

So using myself as an example, the code should be as followed.

name = "Mattias"

subject = "Treehouse loves " + name

But lets say + just doesn't exist and the only way we could complete this was through your original approach. As it is right now, it doesn't fulfill the requirements of the challenge. By just having subject.format(name) as is, it will indeed append/attatch name to {} and workspaces will even show that they have indeed successfully connected upon striking our faithful return key! But, if you call/type out our variable, "subject", it'll just spurt out "Treehouse loves {}" once ya strike Mr. Return. The fix itself is quite simple, just add subject = to the beginning of line three before you call .format().

name = "Frank"                                                                                                                                                                                                                             
subject ="Treehouse loves {}"                                                                                                                                                                                                                                                                                                                                                                                                                                                     
subject = subject.format(name)    

Now if we call "subject" this time around, it should fulfill the requirements of the challenge in a universe where + no longer exists.

But hey, I'm still green so take my wall of text with a grain of salt.

Hope this helps ya out mate!