Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introducing Lists Using Lists Continental

Can't get this code right in this Treehouse challenge

I have seen all the other questions about this code but none of them work for me. Even when I copy past someones answer it still doesn't work.

Can you tell me whats wrong with this code and why?

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

1 Answer

andren
andren
28,538 Points

Your code is quite close, there are just two issues:

  1. To access the index of a list/string you have to use square brackets [] not parenthesis (). So cont(0) should be cont[0].
  2. Python uses indentation (horizontal spacing) to group code. Code that belongs to the if statements needs to be spaced so that it is considered to be one indentation level deeper than the if statement.

If you fix those issues like this:

continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for cont in continents:
    if cont[0] == "A": # Parenthesis changed to square brackets
        print("* " + cont) # Indentation increased

Then your code will work.

Thank you I tried it with [] and () and even {}. Just forgot I had to indent after :

Just when I think I got it.

Thank you.