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 trial

Python Introducing Lists Using Lists Continental

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

trying to figure out how to only list names in a list beginning in a certain letter

i made my list of continents but now it wants me to print only the ones beginning in 'A' im having trouble remembering how to do that. not sure whether to del the names without a or single out the ones WITH 'A' thank you for your help

continents.py
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
    print('* ' + continent) 
Jordan Hoover
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jordan Hoover
Python Web Development Techdegree Graduate 59,268 Points

I think Alex gave a good more detailed explanation.

All you have to do is add the if statement inside of your loop. Make sure to indent properly and you should be good to go.

4 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Sean,

As Jordan Hoover said, you can use if and only print the elements that match your conditional.

Remember that strings behave like lists so you can index into them with square brackets if you want to check that a character at a specific index matches some condition.

Cheers

Alex

Dantee Fluellen
Dantee Fluellen
1,499 Points

hello I also do not understand what I should do. This is what i have but its not right.

for countinent in continents: if continent in continents.lower("a"): print("* " + continents)

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Dantee Fluellen

Please ensure that you always put your code snippets inside valid Markdown. You can click the Markdown Cheatsheet link below to see how to properly format your code. You should also click the preview (eye) button before submission to check that the page will be rendered the way you expect.

Here is what I am guessing your code was before it was mangled by the HTML converter:

for countinent in continents:
    if continent in continents.lower("a"):
        print("* " + continents)

Your first issue is that you have a typo: you have defined countinent as your temporary variable but when you refer to it later you used continent which is undefined. This is why you get this error in your Traceback:

Traceback (most recent call last):
  File "", line 37, in setUp
  File "/workdir/utils/challenge.py", line 24, in execute_source
    exec(src)
  File "", line 12, in 
NameError: name 'continent' is not defined

Next you are asking if continent (e.g., 'Asia') is in continents.lower("a"). This is invalid syntax. lower() is a method on strings, not on lists (remember continents is the list ['Asia', 'South America', etc]. Also, if we take a look at the official documentation for lower(), you'll see that it doesn't take any arguments. You'll see the following traceback for this error:

Traceback (most recent call last):
  File "", line 37, in setUp
  File "/workdir/utils/challenge.py", line 24, in execute_source
    exec(src)
  File "", line 12, in 
AttributeError: 'list' object has no attribute 'lower'

As I mentioned in a previous answer, you can use square brackets to index into strings. So, if we wanted to see if the first character in a string is equal to a particular value we can do this:

my_string = "Hello, world"
if my_string[0] == "h":
    print("It was `h`!")

In this case, 'H' is not equal to 'h' so if statement will not evaluate to True. This is where we use the lower() method to ask for the lowercase version of the first character in my_string:

my_string = "Hello, world"
if my_string[0].lower() == "h":
    print("It was `h`!")

Hopefully this clears up how you need to modify your conditional test.

Finally, you have another typo in your print statement: you are attempting to concatenate (use the + operator) on "* " (a string) and continents (a list of strings). You cannot concatenate lists to strings. I assume you meant to print just one continent and not all of them, in which case you should use continent not continents. This issue will generate a traceback like this:

Traceback (most recent call last):
  File "", line 37, in setUp
  File "/workdir/utils/challenge.py", line 24, in execute_source
    exec(src)
  File "", line 13, in 
TypeError: must be str, not list

Hope that helps

Cheers

Alex