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 trialIan Nazareno
Courses Plus Student 1,253 PointsI'm stuck on this loop task challenge. Unable to figure out the code to print "World" after each word from hellos
hellos = [ "Hello", "Tungjatjeta", "Grüßgott", "Вiтаю", "dobrý den", "hyvää päivää", "你好", "早上好" ] new_word = "World" for word in hellos: if word == ["Hello", "Tungjatjeta", "Grüßgott", "Вiтаю", "dobrý den", "hyvää päivää", "你好", "早上好" ]: print(word)
hellos = [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
new_word = "World"
for word in hellos:
if word == [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
hellos
2 Answers
Rich Zimmerman
24,063 PointsYou just want to concatenate new_word to each item in the list. You can do this with the .format method as well..
for word in hellos:
print("{} {}".format(word, new_word))
When you do
for word in hellos:
the "word" variable is the value of each item in the "hellos" list as it loops through each item in the list.
Ian Nazareno
Courses Plus Student 1,253 PointsThat makes perfect sense. I was wondering if I had to use .format, but wasn't entirely sure. Thanks Richard!
Rich Zimmerman
24,063 Pointsyou can do it that way or like:
for word in hellos:
print(word + " World")
# or
for word in hellos:
print(word + " " + new_word)
The thing i've noticed in the real world, at least at my job, is that .format seems to be the preferred method.