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

Moises Miguel
Moises Miguel
5,028 Points

python for loops. Need help with the "Hello" question

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'm not sure what to do in this challenge or how to do it.

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

Hello, thanks for helping, but its still says its incorrect. I don't think we are supposed to use .format()

https://teamtreehouse.com/library/python-basics/logic-in-python/around-and-around

That is the link to the video. Hopefully this can help you answer it.

Thanks.

3 Answers

Kris Powell
Kris Powell
6,246 Points

Hi! This challenge is asking us to use the 'for' loop, which can loop through each item in a list. For instance, you could use:

for greeting in hellos:
    print("{}, friend!".format(greeting))

to print some friendly greetings.

I hope that helps. Let me know if you are still stuck :) kris

What is the purpose of .format() and why it is using {} in front of "friend"

Kris Powell
Kris Powell
6,246 Points

Ah, thank you, the format() method might not have been introduced yet in this course!

this would be more appropriate:

for greeting in hellos:
    print(greeting + ", friend!")

As for the format() method; in my original example the {} in the string would be replaced by the value of the variable 'greeting'. I hope that isn't too confusing :)

Kris Powell
Kris Powell
6,246 Points

Hi Moises, You're right about format, it shouldn't be used here. Please try my 2nd answer, good luck!