Python

Fernando Secchi
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,145 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
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)