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 trialJason Loveless
726 PointsI'd like you to now only print continents that begin with the letter "A".
Finding this a bit tricky, please could someone show me the correct code, i'm confusing myself ahaa
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent[0,3,5,6] in continents:
print("*", continent)
2 Answers
Scott Bailey
13,190 Pointscontinents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for name in continents:
if "A" == name[0]:
print(f"* {name}")
The for loop will go through each of the items in the list, then the if statement checks each name - if the first letter of the name matches "A" it will print "* name"
Hope this helps!
Grzegorz Zielinski
5,838 PointsI would do it in completely different way.
Here is my FULL solution:
let continents = [
"Asia",
"South America",
"North America",
"Africa",
"Europe",
"Antarctica",
"Australia",
];
let html = "";
for (i = 0; i < continents.length; i += 1) {
// Loop goes through all the index objects
if (continents[i].charAt(0) === "A")
// Checks if there is a name with "A" as a first letter, "chartAt(indexValue)" is a method which checks for you if there is a certain letter standing in a word at specified index position, in this cause "0" cause you want to check first letter.
html += continents[i]; // Adds all the index values with "A" as a first letter to 'html'
}
console.log(html); // prints all 'html' to console
There is few improvements which you could do like ignoring the fact if the first letter is capitalized or not etc. but this is just simple answer for your question.
It prints all the names which starts with "A" in console, if you want it to print it to the page or in any other way just modify "console.log(html)" at the end of script :)
Hope this helps
[EDIT]
Oh my God, I've just noticed that it is a question about script in Python, sorry for that, the solution I've posted here is for JS, my mistake.