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 Second Shopping List App

Valerio Versace
Valerio Versace
11,319 Points

Shopping list While loop

Kenneth does it like this:

show_help()

while True:
    new_item = input("> ")
    if new_item == "DONE":
        break
    elif new_item == "HELP":
        show_help()
        continue
    elif new_item == "SHOW":
        print_list()
        continue
    add_to_list(new_item)

print_list()

Why all those "continue"?

Wouldn't it be better to do it like this?

show_help()

while True:
    new_item = input("> ")
    if new_item == "DONE":
        break
    elif new_item == "HELP":
        show_help()
    elif new_item == "SHOW":
        print_list()
    else:
        add_to_list(new_item)

print_list()

2 Answers

Hi Valerio,

I think the reason why Kenneth worked his code that way is that he's trying to teach how continue works. Your code would be more efficient, though.

Valerio Versace
Valerio Versace
11,319 Points

Cool, didn't think about that! Makes sense.

without the continue, the words HELP and SHOW get added to the list. he briefly explains it at 3:40.

Actually, that isn't the case with Valerio's code.

Valerio moved the add_to_list(new_item) into an else block, so it won't be executed unless DONE, HELP and SHOW are all != to new_item.

ah... i see that