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 trialZelemkhan Saydullaev
13,028 PointsCan'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 = ['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
28,558 PointsYour code is quite close, there are just two issues:
- To access the index of a list/string you have to use square brackets [] not parenthesis (). So
cont(0)
should becont[0]
. - 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 theif
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.
Zelemkhan Saydullaev
13,028 PointsZelemkhan Saydullaev
13,028 PointsThank you I tried it with [] and () and even {}. Just forgot I had to indent after :
Just when I think I got it.
Thank you.