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 trialConor Mosier
925 PointsSo i know there's a better, more efficient, way to do this. Does anyone have any ideas?
continents = [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia', ]
Your code here
for n in continents:
if n == continents[0]:
print("* " + continents[0])
elif n == continents[3]:
print("* " + continents[3])
elif n == continents[5]:
print("* " + continents[5])
elif n == continents[6]:
print("* " + continents[6])
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for n in continents:
if n == continents[0]:
print("* " + continents[0])
elif n == continents[3]:
print("* " + continents[3])
elif n == continents[5]:
print("* " + continents[5])
elif n == continents[6]:
print("* " + continents[6])
2 Answers
jb30
44,806 PointsYou could treat n
as an array and check if n[0] is 'a'
.
amandae
15,727 PointsIf I remember correctly, this is supposed to check for words that start with the letter A. Something like this might work. Each word is a string, which is iterable, so we can call it by index. It is something like a list of lists. If you weren't sure about the case, it would be good to use a .upper or .lower method, but these have consistent formatting, so it doesn't matter. This is the same thing jb30 said, just in different words...
for continent in continents:
if continent[0] == A:
print(continent)