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

Why do we need the 'continue' line in the input IF statement?

in the part of the program where we have:

while True:
    # ask for new items
    new_item = input("> ")

    # be able to quit the app
    if new_item == 'DONE':
        break
    elif new_item == 'HELP':
        show_help()
        continue

why do we need to add the line 'continue', surely at the end of the if statement the while loop would continue anyway?

1 Answer

In that particular example, continue isn't required. The code should work just fine. However, in other situations you just might need a continue in your code to get the desired output.

Ah okay thanks!

Do you have an example?

Actually, there was a good example on the next code challenge:

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

using continue means we skip over the rest of the current loop ( ignoring print(item) ).