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

Cameron Andersen
Cameron Andersen
2,649 Points

this seems right but I just can't get it to work

Any ideas, says my syntax is wrong?

loop.py
hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]
my_string = "World"

for word in hellos:
  print hellos + " " .join(my_string)

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There are syntax errors in your code. The print is a function in Python3 so parens must be used. Also the for loop variable word should be used in the print statement. Using join will intersperse spaces between the letters.

my_string = "World"

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

This can be compressed to

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

EDIT: removed comment about using join with a string argument.

Hi Cameron,

I think what you want is this:

for word in hellos:
  print word + "  " .join(my_string)

In your code, you're trying to print the entire list for each run through the loop. This code prints each hello on a separate line, which I assume is what you're going for.

Hope that helps!

I'm using Python 3.5.1 (OSX) and it works just fine:

Output:

Hello W o r l d
Tungjatjeta W o r l d
Grüßgott W o r l d
Вiтаю W o r l d
dobrý den W o r l d
hyvää päivää W o r l d
你好 W o r l d
早上好 W o r l d
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

That's not what the Challenge says.

Not sure why print without parens is accepted as valid. Python 3.x is supposed to be a function with parens.

I stand corrected on passing a string to join. And will update my answer. Beyond that, using .join is not the correct solution here as it intersperses spaces with all of the characters.

Cameron Andersen
Cameron Andersen
2,649 Points

Thank you everyone for the replies, this is a great help to someone just getting started with the language. I think I need to take better notes because the syntax seems to be tripping me up a bit.