Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,524 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!