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 trialpya medan
59 PointsHelp Me
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
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for continent in continents:
print("* " + continent)
3 Answers
Alan Ayoub
40,294 PointsHi Pya,
The for loop is looking for continent in continents and we need to only print the continents that begin with the letter A.
I personally like to start with Pseudo Code:
// If continent starts with the letter A, print continent
For me, there are two things that we need to do:
- write an if statement
- and reference the first index in the array of continent making it equal to the letter A
When we look for the first letter in any array, we need to reference the first index like so: contient[0]
So in challenge 2, we simply need to add this condition:
if continent[0] == 'A':
Our final code for this challenge looks like this:
for continent in continents:
if continent[0] == 'A':
print("* " + continent)
I hope that helps!
Kevin Langmade
13,896 PointsHi Pya,
This code also works but I think Alan's is a bit cleaner. This code, however, uses the 'in' keyword that was also mentioned in the video.
for continent in continents:
if "A" in continent[0]:
print("* " + continent)
pya medan
59 PointsThanks Alan
Tim Spangler
3,538 PointsTim Spangler
3,538 PointsThanks for the reminder, I blanked on adding an if statement.