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 Python Basics (2015) Shopping List App Continue

I need help with this... Example: ["abc", "xyz"] will just print "xyz".

Question:

Same idea as the last one. My loopy function needs to skip an item this time, though. Loop through each item in items again. If the character at index 0 of the current item is the letter "a", continue to the next one. Otherwise, print out the current member. Example: ["abc", "xyz"] will just print "xyz".

I think python is not for me... Maybe I'm to stupid...

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

1 Answer

andren
andren
28,558 Points

This is a challenge a lot of people end up having issues with so you are not alone, and 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.

Thank you about your time. This is really well explained answer.