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 Introducing Lists Using Lists Continental

Tsering Choedhen
Tsering Choedhen
1,051 Points

Any suggestions how to print continents that begin with letter "A" please?

I am trying to access the string in a list. But i can't think of any. Happy to hear your help.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
for continent in continents:
    print("* " + continent)
while continents[0][0]

2 Answers

boi
boi
14,242 Points

How about you try to use an if condition and not while. I want you to figure this out by yourself so DON'T look at the direct solution below BEFORE reading the indirect solution.

INDIRECT SOLUTION: suppose you have a list of words, and in those list of words I want you to give me words that start with an alphabet "A", so your solution would be something like;

For each word in this list of words, if the alphabet "A" is the first letter in this word, I want that word

DIRECT SOLUTION:

for word in words:
    if "A" == word[0]:
        print(word)

The actual solution to the challenge:

since the instructor wants you to make use of indexing, let's solve using indexing

for continent in continents:

    if "A" == continent[0]:   # Made use of indexing here uisng the "if" condition

        print("* " + continent)

That's it

Tsering Choedhen
Tsering Choedhen
1,051 Points

Thank you @boi for your interesting way of making me to think for the solution.