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

Andreas Wassberg
Andreas Wassberg
5,856 Points

can only concatenate list (not "str") to list Code Challange - Loop

Hi,

I have been working on this problem for several hours and I cant seem to solve this challenge. Can someboy help me?

Challenge Task 1 of 1 I need you to write a for loop that goes through each of the words in hellos and prints each word plus the word "World". So, for example, the first iteration would print "Hello World".

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

a = " World"

for word in hellos: hellos + ', '.join(a) print(hellos) break

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

.join() is for joining the items in an iterable (something that has multiple items). Joining your a variable would produce " ,W,o,r,l,d" which isn't at al what you want. Also, hellos +the join won't do what you're thinking, either.

7 Answers

Ferdinand Pretorius
Ferdinand Pretorius
18,705 Points

Hi Andreas,

You are very close to the answer, here is how I would do it:

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

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

Hope this helps!

Mars Epaecap
Mars Epaecap
5,110 Points

there's a space in her code

" World" in print(word + " World") as oppose to what I had "World"

The space in " World" killed me.

Andreas Wassberg
Andreas Wassberg
5,856 Points

Hi guys,

Thanks for the answer. It worked out fine!

//Andreas

Patric Daniel Pförtner
Patric Daniel Pförtner
1,542 Points

Hi Andreas,

You are very close to the answer, here is how I did it:

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

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

Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :)

Sandeep Krishnan
Sandeep Krishnan
9,730 Points

for word in hellos: print(word {}.format(" World"))

I tried this but it didn't work for me

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You need to concatenate the word variable to the "{}" string.

The problem that got me is in the question not in the code :p

Jason Prince
Jason Prince
5,113 Points
# List of different ways to say "Hello".
hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]

# Each time the loop cycles it will assign one of the list items to the variable hello. 
# It will do this in order of left to right until the end of the list.
# Example: 1st loop, hello = "Hello". 2nd loop, hello = "Tungjatjeta". 3rd loop, hello = "Grüßgott". 
for hello in hellos:  
        greeting = hello + " World"  # Concatenate the list item variable to the string " World".
        print(greeting)  # Displays the concatenated string
David Cox
David Cox
671 Points

I was having trouble with this until I looked for help. I had it correct but why do we need to put a space between the double quote and World?