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 trialRusell Shamu
4,592 PointsIteration
I'm stuck in this challenge.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print("* " + continent)
Rusell Shamu
4,592 Points#Part 2: Print continents that only start with the letter "A"
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
if "A" in continent:
print("* " + continent)
2 Answers
Eric M
11,546 PointsHi Rusell,
Thanks for posting the code that was not working for you, this makes it much easier to identify what is going on.
In your code:
for continent in continents:
if "A" in continent:
print("* " + continent)
You are looping through all the entries in continents and printing out an entry if it has a capital A in it. The challenge, however, is asking you to only print continents that begin with the letter "A".
Your code will find a capital A in "North America" for example, a continent that starts with N.
You need to change your if condition to only be true if the first letter is a capital A.
Python represents a String as a list of characters (I think it might actually be a list of strings of length 1 but that's not important right now) what this means is that you can access list items using an index, so continent[0]
is the first character in the String.
If you change your if condition from if "A" in continent:
to if "A" == continent[0]:
you'll be checking only the first character instead of seeing if a capital A appears anywhere in the String.
Rusell Shamu
4,592 Pointshi emck, Thank you so much. I got it now.
Eric M
11,546 PointsHi Rusell,
The code you posted is valid for the first part of this challenge. (I was able to copy paste it and pass the challenge). If you're having trouble with the second part of the challenge let us know what's tripping you up, otherwise read on.
Sometimes the python interpretation is not clear about whitespace errors, it may be that the markdown on the forums is stripping unnecessary whitespace from your solution that's throwing off the Treehouse interpreter. It could also be that there's a cached (failed) solution you submitted that's preventing the new one from being properly checked.
Try hitting restart
on the challenge and copy pasting your code from this forum question. If that doesn't work you might need to log off Treehouse, restart your browser, and come back. I had to do this for a couple of challenges in the Python track and while it was frustrating, the vast majority of them worked without issue.
Cheers and good luck
Rusell Shamu
4,592 PointsHie
Indeed I am having trouble with the second part of the challenge. Given the code provided above, I am being tasked to print only continents that begin with the letter 'A'. The way I am doing it doesn't feel right. I am repeating my code 'print(continent[0])' but only changing the appropriate index for a particular continent for each print statement. Perhaps that explains why my code is failing in this challenge.
Please assist
Eric M
11,546 PointsEric M
11,546 PointsPost the code you've written for part 2 that's giving you the trouble