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 trialSanjay Devadkar
Courses Plus Student 1,016 PointsI'd like you to now only print continents that begin with the letter "A". HINT: Remember that you can access characters
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))
print(A)
3 Answers
Alissa Kuzina
5,835 PointsHey! This is how i did it
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continent[0] == 'A':
print("* " + continent)
The thing is that you need continents where first letter is 'A'. So we can iterate throug each continent and access first letter in it, In every iteration we have continent, If my condition is true I print this word on the screen .
Elgene Ee
3,554 PointsHi Sanjay, try this out, using 2 'for loops' seems kinda impractical and long codes. However it works, take some time to dissect what I am doing, you can do it!
my_list = []
char = 'A'
for item in continents:
if item[0] in char:
my_list.append(item)
else:
continue
for continent in my_list:
print('* ' + continent)
Sanjay Devadkar
Courses Plus Student 1,016 Pointsis there any other best way to do achieve this.
Elgene Ee
3,554 PointsYes ofc there are other ways to solve this problem, think programming more like a mathematical question, it is about solving problems, not memorizing to solve. If you ask me the best way, there are certainly no best way to solve, everyone has their own style, make sure you grasp the basics fundamentals of Python, and make yourself alot of python program until you get the hang of it...You can do it!
Luke Shephard Zikuyumo
1,655 Pointsmy_list = [] char = 'A' for item in continents: if item[0] in char: my_list.append(item) else: continue for continent in my_list: print('* ' + continent) It's still gives me a bummer
Laxmi Suwal
Python Development Techdegree Student 1,523 Pointsif continent[0] == 'A': print("* {}".format(continent))
Sanjay Devadkar
Courses Plus Student 1,016 PointsSanjay Devadkar
Courses Plus Student 1,016 PointsCan you explain the code, As per my understanding if you see your code line## if continenet[0] == 'A': statement## it should only give Asia as it is at zero position right.
Alissa Kuzina
5,835 PointsAlissa Kuzina
5,835 PointsNo, you receive Asia , Africa, Antarctica, Australia and you can see it if you run this code . The thing is that with for loop I "touch" every "continent" in "continents". With continent[0] I access the first letter of each "continent" ( in Asia - A, in South America - S, in North America - N and so on). Then with comparison opeator I check what is A. Then I print the name of continent where results were equal (A=A).
Ralph Wright
5,869 PointsRalph Wright
5,869 PointsThank you so much for this! This is very clever actually. Great Job!
Thomas Beaudry
29,084 PointsThomas Beaudry
29,084 PointsNicely done Alissa!!
Patrick Altman
6,173 PointsPatrick Altman
6,173 PointsThank you Alissa.