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) Logic in Python Loop

Unable to concatenate List containing Strings with String.

I am working on the loop.py exercise, in which I have to create a For loop that will Print each String within the List called hellos and append the String "World" via concatenation.

I'm able to get the Strings in hellos to print without issue but run into trouble when I add + "World".

When I run the code, the String "Hello" will output and then I get the following error:

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

If I'm reading this correctly, I need to change the list from NoneType to String, but I'm not sure how.

loop.py
hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]
for word in hellos:
    print(word) + "World"

1 Answer

andren
andren
28,558 Points

The issue is actually that you are trying to append "World" to what the print function returns, rather than the word string due to the + being placed outside the parenthesis of the print call.

If you place it inside the parenthesis then you won't get that error, but there is another issue. Which is the fact that when you concatenate a string the two strings are glued together without any spaces. So "Hello" + "World" for example would produce "HelloWorld", in order to get correct spacing you have to manually add a space.

Like this for example:

for word in hellos:
    print(word + " World")

Ah, it's so clear now! Thank you!