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 trialLuke Maschoff
820 PointsVery confused on this task/challenge
It says to index to a certain character, but I do not know how to index a character. Are they asking me to just manually count each letter out, so I start with continents[0]? Or is there some other way to call every single continent that starts with the letter "A" into a print function?
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
print("Continents:")
for continent in continents:
print("* " + continent)
print(continents[0], continents [)
2 Answers
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsHey Luke, the challenge is asking you to do the same thing that you did in the first challenge, except this time you only print out continents in the list that start with "A". Luckily, Python has a handy little built-in function called startswith()
that you can use. Try setting up a conditional and checking each continent if it starts with "A": if continent.startswith("A"): print("*" + continent)
. Good luck!
Gregory James
6,537 Pointsso basically the startswith() funcion only recognizes the indexes that Start With, for example "X"?
Luke Maschoff
820 PointsThat is a good answer and thanks, its just that we havn't learned that yet in this track. Any other way of doing it that I have learned? Also, I tried the startswith() feature and it still didn't work?
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsI was able to pass the challenge using the startswith()
method. Another way is just using the index. I prefer startswith()
because it seems cleaner to me, but either way works. if continent[0] == 'A': print("* " + continent)
.
Luke Maschoff
820 PointsHmm. Michael it still isn't working. Let me show you what I have and maybe you could help me out:
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
print("Continents:")
for continent in continents:
print("* " + continent)
if continent.startswith("A"):
print("* " + continent)
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsGlad you figured it out Luke Maschoff and glad I could be of service. Good luck!
Cheo R
37,150 PointsLuke, you're close, just need to indent correctly and remove the first print statement.
Luke Maschoff
820 PointsGot it! Thanks a lot! I didn't realize you could include if statements in for/in statements. Thank you to everyone who helped me!
Cheo R
37,150 PointsCheo R
37,150 PointsYou're close. You just need to replace your code with test condition.
Only print the continent if the first letter begins with an "A".
You can index a string like a list. For example:
print("string"[0]) # "s"