Python Introducing Lists Using Lists Continental

Luke Maschoff
Luke Maschoff
820 Points

Very confused on this task/challenge

It says to index to a certain character, but I do not know how to index a character. Are they asking me to just manually count each letter out, so I start with continents[0]? Or is there some other way to call every single continent that starts with the letter "A" into a print function?

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

You're close. You just need to replace your code with test condition.

Only print the continent if the first letter begins with an "A".

You can index a string like a list. For example:

print("string"[0])  # "s"

2 Answers

Michael Cook
Michael Cook
16,029 Points

Hey Luke, the challenge is asking you to do the same thing that you did in the first challenge, except this time you only print out continents in the list that start with "A". Luckily, Python has a handy little built-in function called startswith() that you can use. Try setting up a conditional and checking each continent if it starts with "A": if continent.startswith("A"): print("*" + continent). Good luck!

so basically the startswith() funcion only recognizes the indexes that Start With, for example "X"?

Luke Maschoff
Luke Maschoff
820 Points

That is a good answer and thanks, its just that we havn't learned that yet in this track. Any other way of doing it that I have learned? Also, I tried the startswith() feature and it still didn't work?

Michael Cook
Michael Cook
16,029 Points

I was able to pass the challenge using the startswith() method. Another way is just using the index. I prefer startswith() because it seems cleaner to me, but either way works. if continent[0] == 'A': print("* " + continent).

Luke Maschoff
Luke Maschoff
820 Points

Hmm. Michael it still isn't working. Let me show you what I have and maybe you could help me out:

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

if continent.startswith("A"):
    print("* " + continent)
Michael Cook
Michael Cook
16,029 Points

Glad you figured it out Luke Maschoff and glad I could be of service. Good luck!

Cheo R
Cheo R
37,145 Points

Luke, you're close, just need to indent correctly and remove the first print statement.

Luke Maschoff
Luke Maschoff
820 Points

Got it! Thanks a lot! I didn't realize you could include if statements in for/in statements. Thank you to everyone who helped me!