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

john herron
john herron
2,485 Points

Stuck on the loop challenge in Python

The challenge says I need to loop through a list and add the word 'World' after each item in the list

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

2 Answers

andren
andren
28,558 Points

That can be accomplished pretty simply with a for loop

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

A for loop takes a list and then takes each item and assigns it a variable within the loop, in the example above I choose to name the variable "word".

The first time though the loop word will be set to the first item in the list, the second time it will be set to the second item, etc, and then it will automatically end once it has gone through all of the items.

Inside of the loop I'm just taking whatever the word is and then appending " World" to it, the space is needed since the words would run together without it.

Ansoumane Kadher Kande
Ansoumane Kadher Kande
2,484 Points

Thank for help i had the same question.