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 trialSehrish Ijaz
756 Pointshow to retrieve continents starting with letter A?
i don't know the code in for loop to get elements with a specific letter
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
2 Answers
Sehrish Ijaz
756 Pointstinents = [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia', ]
Your code here
for continent in continents: print("*",continent)
Steven Parker
231,236 PointsYou'll still need the code you did for task 1 that creates the loop, you will just add an "if" statement to control when the "print" happens.
You can access individual characters in a string by indexing, using brackets similar to working with a list. For example, since index numbers start at 0, the first character of "mystring" would be "mystring[0]
".
You could then compare that to the letter "A" in a conditional expression as part of the "if" statement.
Sehrish Ijaz
756 PointsCan you please send me the whole code?
Steven Parker
231,236 PointsPlease post the code you passed task 1 with first, then I can show you what you need to add for task 2.
Steven Parker
231,236 PointsSo between the "for" line and the "print", you could add:
if continent[0] == "A":
And be sure to adjust the indenting of the "print" line.