Python Python Basics (2015) Shopping List App Continue

I don't understand this exercise. I did what I could. Help please!!

Maybe the indentation is wrong, I don't know

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

2 Answers

andren
MOD
andren
Treehouse Moderator 28,325 Points

Your code is actually very close to correct. The issue is that the index method does not function in the way you might expect, it is actually used to find the index of a character, not to get something from an index.

Meaning that it expects to be passed something like a letter and will then tell you what index that letter is at.

So to check if a was at index 0 using the index method you would have to reverse the current logic of your code like this:

item.index("a") == 0

However the index method is not really well suited for this task to begin with, because it will throw an exception (basically an error) if it does not find the character you specify.

The ideal way to check the first letter in this task is to use bracket notation. With bracket notation you can just type the name of a string or list followed by a pair of brackets with an index. Like this:

item[0]

And that will return the letter at that index.

If you use that in your code like this:

def loopy(items):
    for item in items: 
        if item[0] == "a":
            continue 
        else: 
            print(item)

Then your code will work.

Thanks

Try indexing your string like so.

item[0]

Thanks