Python Python Basics (2015) Shopping List App Continue

Ben Hedgepeth
Ben Hedgepeth
Python Web Development Techdegree Student 6,741 Points

Using Continue while iterating a list

I'm not clear on what I'm doing wrong with my code. I'm testing whether the first character of each item in the list starts with 'a' but the challenge isn't passing.

breaks.py
def loopy(items):
    for item in items:
        if item.index("a") == 0:
            continue
        else:
            print(item)

2 Answers

Antonio De Rose
Antonio De Rose
20,840 Points
def loopy(items):
    for item in items:
        if item.index("a") == 0: #this will actually return, the index, had there been an element with the value "a"
#that too you cannot use "==", if at all to find the index of an element of "a" - item.index("a")
#close, give it a try, hint - square bracket
            continue
        else:
            print(item)
Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! Remember that if the substring is not found at all it will raise an exception and the function will terminate. So if we send in an item like "test", then this will fail. There is another way to check if the first letter is an "a". Strings can have the individual characters accessed through subscripting just like the items in a list.

Here's an example:

my_string = "Treehouse"
print(my_string[4])

This would print the letter "h". I think you can get it with these hints, but let me know if you're still stuck! :sparkles: