Python Introducing Lists Using Lists Continental

Rigby Adams
Rigby Adams
970 Points

How do I do this?????

I can't seem to figure this one out. Any help would be appreciated

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for [0,3,5,6] in continents:
    print('* ' + continent)
Rigby Adams
Rigby Adams
970 Points

Challenge 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

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,264 Points

You 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.