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

Fernando Secchi
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernando Secchi
Python Web Development Techdegree Student 2,626 Points

Why my code it's not working in the challenge? Works fine in my local machine and in workspace.

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

2 Answers

Ben Reynolds
Ben Reynolds
35,170 Points

My suspicion is that this line is the culprit:

if item.index("a")==0:

If the string does not contain 'a' at all, I'm pretty sure the index() method raises an exception. I'd suggest rewriting the "if " statement to use square-bracket syntax to check if the character at the first index is 'a' instead of using the index() method to see what the index of 'a' is.

Fernando Secchi
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernando Secchi
Python Web Development Techdegree Student 2,626 Points

I wasn't catching the error:

for item in items:
    try:
        if item.index("a") == 0:
            continue
    except ValueError:
        print(item)
    else:
        print(item)