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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Auceanne Richer](https://secure.gravatar.com/avatar/a2429c3f543838e2d8e785538c551cc0?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Auceanne Richer
513 PointsWhy can't I add the index to the end of the print for-in loop?
I'm not sure why the list isn't printing the for-in loop, where I added the index at the end of my print for-in loop. Why doesn't this work?
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
print("Continents:")
for continent in continents:
print("* " + continents[0, 3, 5, 6])
1 Answer
![Megan Amendola](https://uploads.teamtreehouse.com/production/profile-photos/7340382/micro_headshot.jpg)
Megan Amendola
Treehouse TeacherHi Auceanne Richer ! When you pass something into a list (like my_list[something]
), it can only be an integer or a slice. What you have passed into the list right now is a tuple (which you will learn about later if you're on the Python track). This will cause its own error.
Let me break down what happens in your loop. If I print continents, I am printing out the whole list equal to the number of items in the list.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print(continents)
# console
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
>>> [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]
This is because a loop repeats equal to the number of items in the list (or other iterable) you give it. Here the iterable is the continents list which has 7 items in it, so the loop repeats 7 times and does whatever is inside of the loop 7 times.
What the challenge wants you to do is print out a bulleted list of all of the continents using a loop. If we instead print out continent
instead of continents
, this is what you'll see.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print(continent)
# console
>>> Asia
>>> South America
>>> North America
>>> Africa
>>> Europe
>>> Antartica
>>> Australia
Now, you get each continent printed out to the console because continent
changes each loop to be the next item in the loop. Then you just need to add '* ' +
to get them as bullets.
The second task asks you to only print out continents that start with 'A'. In your loop, continent is a string because you have accessed an individual string from the list. With strings, you can use index notation to determine what the first letter is, like this:
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print('* ' + continent[0])
# console
>>> * A
>>> * S
>>> * N
>>> * A
>>> * E
>>> * A
>>> * A
Now all you need to do is print out the continent only if the first letter is 'A'.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
# if first letter equals 'A'
print('* ' + continent)
Hopefully, this helps. Let me know if you're still stuck