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 trialAjet Shabani
439 PointsI'd like you to now only print continents that begin with the letter "A". someone explain me this
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print ('*', continent)
2 Answers
Steven Parker
231,269 PointsBy indexing with brackets, you can isolate the first character of a string. For example, the first letter of name would be name[0]. Then, it's a simple matter to compare it to what you are looking for.
Asher Orr
Python Development Techdegree Graduate 9,409 PointsHi Ajet!
In challenge task 1 of 2, you looped through each continent in the list "continents" and printed it next to a bullet point. Nice work!
For challenge task 2, the one you're asking about, you need to modify the code so that it prints out the continent ONLY if the first letter starts with A.
Your code needs a conditional statement to make this work. Conditional statements use the operators if, elif, and else.
Here's an example:
x = "Ajet"
if x == "Ajet":
print("The variable x stores the value 'Ajet'")
else:
print("The variable x does not store the value 'Ajet'")
If you ran this code, "The variable x stores the value 'Ajet'" would print to the console.
But if you changed x to "Asher", like this:
x = "Asher"
if x == "Ajet":
print("The variable x stores the value 'Ajet'")
else:
print("The variable x does not store the value 'Ajet'")
"The variable x does not store the value 'Ajet'" would print to the console.
To solve the challenge, you need to print the continent only if the first character is A. Remember that you can access characters in a string by index. Let me show you an example.
my_list = [
'one',
'two',
'three',
'four',
'five',
]
for number in my_list:
print(number[0])
The code above would print this to the console:
o
t
t
f
f
To solve the challenge, your code should incorporate both concepts (conditional statements and accessing characters in a string by index.)
I hope this helps! Let me know if you have additional questions.
samsrose
3,691 PointsAfter seeing your comment, it seemed like a no-brainer. Thank you for clarifying this.