Python Python Basics (2015) Logic in Python Loop

struggling with loops if someone can help me

so how do i get world to appear after everything in a list or string, using a for loop? I'm very confused with loops right now :(

loop.py
hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]
for hello in hellos:
    print('world')
    print(hello)

4 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,102 Points

Hi Jason,

You're nearly there with that. You can add the strings together or use a formatter:

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

# or

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

I hope that helps,

Steve.

thank you so much!

Nick Cannariato
Nick Cannariato
25,823 Points

You're really close!

So, to start, let's talk about how to think through loops. Every line of your loop is a "step" for your instruction that will repeat. So right now, the way your loop is structured is like this (what you're about to see isn't actual Python code):

for every word in this list of words:
    Step 1: print the word "world"
    Step 2: print the current word we're at in the list
    # REPEAT

Since your print instructions are on different lines, they're read as different steps, so they print on different lines. Your output would look like:

World
Hello
World
Tungjatjeta
# etc

What you need to do is rewrite your loop so that it looks like this:

for every word in this list of words:
    Step 1: print(the the current word we're at in the list + "World"
    # REPEAT

Take a minute and think through it, then let me know if you're still stuck or have questions.

thank you all for the replies it does help me a lot! i'm new to python so i hope its a little normal to get stuck on loops lol

Steve Hunter
Steve Hunter
Treehouse Moderator 56,102 Points

Fear not - I got stuck on pretty much everything! It all becomes clearer with repetition over the courses and there's always help on hand in the Community pages. :zap: :dizzy:

Nick Cannariato
Nick Cannariato
25,823 Points

It was definitely my experience Jason. It took me a bit to learn to think in loops, but the more you practice the more you'll understand. Just keep at it!

thanks i feel better haha, I understand 90% of what i'm learning just loops threw me a little curve ball! But like you said with practice it all becomes easier!