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 trialAli Dahud
3,459 Pointswhat's wrong with my code?
I'd like you to now only print continents that begin with the letter "A".
HINT: Remember that you can access characters in a string by index Bummer! : Whoops! I found a country that didn't start with A in your output
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for continent in continents:
print("* " + continent)
for continent in continents:
for letter in continent:
if letter[0]=='A':
print(continent)
9 Answers
Daniel Boisselle
Front End Web Development Techdegree Student 17,438 Pointscontinents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for continent in continents:
if continent[0] == "A": print("* " + continent)
You just need a simple check seeing if the first character is == "A"
Each value in the continents list will be assigned to the "continent" value in the for loop. From there you can access the first character by indexing it at the 0th index:
continent[0]
Hope this helps! Dan
Marcus Edmondson
9,454 PointsTask 2 wasn't really clear about adding the asterisk in the print statement. It seemed to only ask to print out continents that start with A.
tk19
10,006 PointsThe Print needs to be nested in the if statement. (If you paste that code, make sure you press enter for it to be correctly spaced onto the next line. If you manually tab then it won't compile correctly.)
Amanda Perez
Courses Plus Student 2,056 Pointsthis still produces an "oops"
Antero Perez-Solis
1,444 PointsSame here
jon nikolakakis
1,639 PointsDaniel Solution works for me
for x in continents:
if x[0] =='A':
print("* " + x)
Martin Ludvigsen
1,734 PointsTricky one as there is one item in the list that begins with A and is not a continent
ayawovi Badohoun
10,334 Pointsmy_list = [] char = 'A' for item in continents: if item[0] in char: my_list.append(item) else: continue for continent in my_list: print('* ' + continent)
Bobbi Seesz
2,272 PointsYou have to add the 'if' statement to the first code you did, instead of making two separate blocks.
for continent in continents: if continent[0] =='A': print("* " + continent)
Karyna Reut
1,611 Pointscontinents = [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia', ] for a_letter in continents: if a_letter[0] == "A": print ("*" + a_letter)
Spent so long doing my code, in the end, it ended up being right in PyCharm, but shows false in Workspace.
namdy
2,915 Pointsnamdy
2,915 PointsStill doesn't pass
Bummer: AssertionError: 'South America' unexpectedly found in '* Asia\n* South America\n* North America\n* Africa\n* Europe\n* Antarctica\n* Australia\n* Asia\n* Africa\n* Antarctica\n* Australia' : Whoops! I found a country that didn't start with A in your output