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

Why did it work?

So i looked at my previous challenge, and changed the code a bit to pass the challenge. If i understand correctly then i don't need to add any other condition in order for code to skip the item in list, tried to add elif and else but that was unnecessary. Is it as simple as it looks?

def loopy(items):

for i in items:

    if i[0] == "a":

        continue

    print(i)

1 Answer

Stuart Wright
Stuart Wright
41,119 Points

Your code is correct and it is indeed that simple. Some people would prefer to add an else statement to make it a little clearer to read what's happening. For example, this version has one extra line compared to yours and is also correct:

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

Your version has the benefit of being more concise, which is a good thing, and the version I've posted has the benefit of possibly being a little easier to understand. It's really personal preference.

But the else block is completely unnecessary in terms of functionality. Because 'continue' tells the program to jump ahead to the next item in the loop, your print statement will never be reached if the 'if' condition is satisfied. But if the condition is not satisfied, the program will move straight to your print statement, meaning the else statement isn't necessary (but still correct if you prefer to include it).