Ben HedgepethPython Web Development Techdegree Student 5,767 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.
def loopy(items): for item in items: if item.index("a") == 0: continue else: print(item)
Antonio De Rose20,230 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 NordellTreehouse Moderator
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)
This would print the letter "h". I think you can get it with these hints, but let me know if you're still stuck!