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 trialAsher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsHow do you print certain thing off of a list?
It wants me to only print continents that begin with "A"
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
print("* " + continent)
print(A)
10 Answers
Michael Moore
7,121 Pointscontinents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continent[0] == 'A':
print(continent)
Sorry. Answering this from my phone. It's not printing out as I intend. But I tested it out.
You treat the string the same as you treat the dictionary essentially. Continent[0] is the first letter of the dictionary ['A', 's', 'i','a'] essentially. Hope that helps.
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsYou just have to keep the print("* " + continent)
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsIt should look like this
for continent in continents: if continent[0] == 'A': print(β* β + continent)
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsThanks but didn't work so the questions is
this is the second question. 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
this my code and it says I should keep the previous code and add to it to print out all the continents beginning with A
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia'] for continent in continents: print("* " + continent)
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsThanks I figured it out it worked
Artjom Dzug
8,238 PointsI dont understand i tried like this print(continents[A])
but doesnt work :(
Artjom Dzug
8,238 PointsThank you but can you explain why is there a [0] ? That little thing i dont understand
Michael Moore
7,121 PointsSo if you just looked at continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia'], continents[0] is Asia, continents[1] is South America, continents[2] is North America, and so on. Now to handle every item in the continents dictionary, you say
for continent in continents:
print(continent)
This would print out every continent individually, one at a time, going through the dictionary 'continents'. Now we want to print out every continent that begins with the letter 'A', so you will treat each individual word as its own dictionary. For instance, if we said,
for continent in continents:
for letters in continent:
print(letters)
That would print out, on a bunch of seperate lines, each individual letter of every continent. A, S, I, A, S, O, U, T, H, A, M, E, R, I, C, A, and so on. For continent in continents cycles through the words, so when it gets to Asia, 'for letters in continent:' cycles through the letters of Asia, treating the word as its own dictionary of letters = ['A', 's', 'i', a']. Then on the next time through it would go to South America, then 'letters in continent:' would represent, ['S', 'o', 'u', 't', 'h', 'A', 'm', 'e', 'r', 'i', 'c', 'a'], and it would continue do the list til it reached the end. It is treating each word as its own dictionary. So if you only wanted to print out the ones that begin with A
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continent[0] == 'A':
print(continent)
This goes to the first position of every word. "continent in continents", where continent is each individual string, 'Asia', 'South America', and so on. if continent[0], aka, the first letter of dictionary, 'A', 's', 'i', 'a', is == A, then print the whole string that is represented by the variable 'continent' on that specific cycle through the dictionary.
I'm not sure if I am helping you or making you more confused, but hope that gives a little more explanation.
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsFrom my understanding itβs because it signifying that the element is a list
I had a lot of trouble with that to be honest
Artjom Dzug
8,238 Pointsthat makes sense, its just i know if we put a number there is points a specific word or number in the list thats why i was confused
Asher Goldsby
Full Stack JavaScript Techdegree Student 4,055 PointsYa this one confused me as wel