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 trialjenniferj
Data Analysis Techdegree Graduate 9,026 PointsStuck on challenge task asking for list items starting with A
Hi everyone! I am stuck on the challenge item asking for continents list with only continents starting with A. I went back to the video and it is not in there. I then tried to research online but I didn't find the right code. Finally, I tried using remove to take the items out of the list that didn't begin with A (which I know is neither DRY nor correct...but maybe tricky for an attempt?) Anyway, I'm stuck. I would appreciate some help. Thanks!!
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
print("continents: ")
for continent in continents:
print("* " + continent)
A_list1 = continents.remove("South America")
A_list2 = A_list1.remove("North America")
A_list3 = A_list2.remove("Europe")
print(A_list3)
2 Answers
jb30
44,807 PointsThe hint given for challenge 2 is HINT: Remember that you can access characters in a string by index
. In particular, continent[0]
gives the first letter of the continent
.
Try using an if
statement to compare the first letter to "A"
before printing the continent name.
A_list1 = continents.remove("South America")
removes "South America"
from continents
, and sets A_list1
to None. A_list2 = A_list1.remove("North America")
attempts to call remove()
on None
, which gives an AttributeError: 'NoneType' object has no attribute 'remove'
.
To modify the continents
array,
continents.remove("South America")
continents.remove("North America")
continents.remove("Europe")
print(continents)
This does not pass the challenge, since the continents
array isn't meant to be modified and the array isn't printed in the format specified in the first part of the challenge. You could instead create a copy of the continents
array and remove elements from the copy without modifying the original continents
array.
continentsA = continents[:]
continentsA.remove("South America")
continentsA.remove("North America")
continentsA.remove("Europe")
for continent in continentsA:
print("* " + continent)
jenniferj
Data Analysis Techdegree Graduate 9,026 PointsThank you very much for offering me support. This is my very first course in Python, and I wasn't really having any difficulties until now. I saw the hint, but I honestly don't have a clue what access characters by index means. I have watched all the videos and completed all the tests and exercises, but I am really not understanding this one. Thanks again for your help.