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 trialJason Pallone
895 Pointsstruggling 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 :(
hellos = [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
for hello in hellos:
print('world')
print(hello)
4 Answers
Steve Hunter
57,712 PointsHi 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.
Nick Cannariato
25,823 PointsYou'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.
Nick Cannariato
25,823 PointsHaha, looks like I took a bit too long on my reply. Glad you figured it out!
Steve Hunter
57,712 Points
Jason Pallone
895 Pointsthank 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
57,712 PointsFear 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.
Nick Cannariato
25,823 PointsIt 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!
Jason Pallone
895 Pointsthanks 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!
Jason Pallone
895 PointsJason Pallone
895 Pointsthank you so much!