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
sandhya sivasankaran
1,711 PointsPython: Challenge task - looping through a list
Hi - The challenge task is the below
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".
I understand why it's not giving what i want (the code i wrote is below), although i am not able to figure out what to do and the course is not letting me go beyond this, can someone help me please?
What i wrote is
hellos = [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
for hello in hellos:
str1 = ''.join(hellos)
print(str1 +' World')
break
3 Answers
Charlie Gallentine
12,092 PointsI think you may be overthinking it a little bit, the code can be done as:
hellos = [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
for hello in hellos:
print(hello + " World")
Your code is creating a single string of every value in the list. Then appending "World", then exiting the loop. It is never looping through again as 'break' cancels out of a loop when called.
The output which should be given is: "Hello World" "Tungjatjeta World" "Grubgott World" etc...
sandhya sivasankaran
1,711 Pointshi Alexis, 'hello' is the variable being used in the FOR loop to loop through your list ('hellos), until the FOR loop ends your variable is temporarily stored in 'hello' , that'y why we are using hello instead of 'hellos'. I hope that helps.
Alexis Marrero
4,275 PointsHo nice , i understand now very helpful thank you so much Sandhya.
sandhya sivasankaran
1,711 PointsThat helped. Thank you Charlie.
Alexis Marrero
4,275 PointsAlexis Marrero
4,275 Pointshello, I'm new to this and i was wondering why calling
print(hello + " World")instead ofprint(hellos + " World")? since variable hello has no value in it. i'm very confused right now please help.