Python Introducing Lists Using Lists Continental

Only printing out continents that begin with the letter "A". Can someone help me out?

I am on Task 2 of this challenge. I understand they want me to set up the code so that only the continents names that begin with the letter "A" are printed out (Asia, Africa, Antarctica, Australia). The "HINT" says that indexing is involved. I have tried everything I can think of regarding indexing that I've learned from the previous chapters, but I keep getting told what I'm doing is incorrect. Apparently I'm not supposed to "pop" items off the list (the program checker says that's incorrect). And no other combination of things I've used seems to work. If anyone can give me an extra hint or advice on what I'm missing, PLEASE help me. Thank you.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    print("* {}".format(continent))

4 Answers

Hey Michael!, check this out.

for continent in continents:
    if continent[0] == "A":          # I used indexing here, also remember that indexing starts off with 0.
        print("* " + continent)

Did this help? And most importantly, did you understand the code?

Thank you, Boi! Just tried it out and works perfectly. I knew the solution was something simple, but I originally ended up overthinking it. Will be going back over notes (and writing this info down in notes) to better commit it to memory. I really your help.

Ave Nurme
Ave Nurme
14,779 Points

Hey all!

This may be a bit advanced code for beginners but this also works (please check the code below). NOTE: I am using f-string here instead of .format. It's the newest Python syntax to do string formatting (available since Python 3.6). You can google it and check it out. Happy coding! :-)

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]

for continent in continents:
    if continent.startswith('A'):
        print(f'* {continent}')

"""
Output:

* Asia
* Africa
* Antarctica
* Australia
"""

Hello Ave,

This code you've shown is a bit more advanced than what I'm currently studying (as you yourself mentioned). That being said, it's nice to know of other solutions, and get a glimpse of what I may be learning in the future. Thank you for your response.

Ave Nurme
Ave Nurme
14,779 Points

No worries! :-) I do advise you to look into f-string as you might bump into it in your further studies here at Treehouse. I took the Python Basics course here about 3 weeks ago and at the moment taking the advanced course. F-string is actually easier to write down and also easier to read. Good luck with your studies!

Trace Harris
seal-mask
.a{fill-rule:evenodd;}techdegree
Trace Harris
Python Web Development Techdegree Student 19,216 Points

@Michael, looks like the you have the answer, and even in a couple of different ways. Awesome!! just a couple of things index [0] will always be the first value in an iterable. What Ave has done is taken a string method from Pythons standard library https://docs.python.org/3/library/I would take a look because as a python developer the standard library is invaluable. Containing methods and operations on everything from "strings" to HTTP Request and Response objects. and here is a huge bonus. It's built into the language!