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 trialRigby Adams
1,231 PointsHow do I do this?????
I can't seem to figure this one out. Any help would be appreciated
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for [0,3,5,6] in continents:
print('* ' + continent)
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsYou are using a cool/unique approach, we can build on that. One of the things I love about Python is there isn't just one solution, but many right ways to do it.
To fix your loop:
What you need to do is switch up the "for" loop an put the loop index variable in the first position (I will use the variable named 'i') and the iteration range in the second part of the loop. Then in the print statement, we use the continents list with square brackets to indicate which element of the continents list to print.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for i in [0,3,5,6]:
print("* " + continents[i])
Below is an alternate approach that could serve your purposes too.
This is not the solution to the challenge, but should give you a hint at what code you also could have used to solve the Challenges. I am demonstrating using cities rather than continents so as not to spoil it.
By the way you can do Challenges as many times as you like, with different approaches. So don't forget that you can go back an quiz yourself later on!
Part 1.
cities = ['Portland','Seattle','San Francisco','Los Angeles','Atlanta','San Antonio']
for city in cities:
# notice that we loop through all the cities. Each time the variable city will have
# the value of the next next city in the list.
print("* " + city)
Part 2.
# now, print only cities that start with 'S'
for city in cities:
# here, we check if the first element of the city is the letter 'S'
if city[0] == 'S':
# the first letter of the city is 'S', print it out.
print("* " + city)
Good luck with your Python journey! It can lead to many cool new opportunities to geek out with code and problem solving.
Rigby Adams
1,231 PointsRigby Adams
1,231 PointsChallenge Task 2 of 2 I'd like you to now only print continents that begin with the letter "A".
HINT: Remember that you can access characters in a string by index
: this is the prompt