Python Introducing Lists Build an Application Add Items

My function isn't displaying when I run my code. It isn't showing me the number of items, or how many items. Any ideas?

shopping_list = []

def add_to_list(item): shopping_list.append(item) print("Added. Your list has {} items.".format(len(shopping_list)))

def show_help(): print("What should we pick up at the store?") print(""" Enter 'DONE' when you are finished entering items. Enter 'HELP' for help with this application. """)

show_help() while True: new_item = input("> ")

if new_item == 'DONE':
    break
elif new_item == 'HELP':
    show_help()
    continue
    add_to_list(new_item)

Found it. Indentation error.

2 Answers

Steven Parker
Steven Parker
195,376 Points

For the benefit of other readers, the line after "continue" (that calls "add_to_list") should not be indented as far.

Also, when posting code, use Markdown formatting to preserve the code appearance (particularly indentation).

And an even better alternative to posting code is to make a snapshot of your workspace and post the link to it here.

Shanil Mohan R
Shanil Mohan R
1,492 Points

you have added the function call add_to_list(new_item) in the elif block which will get executed only if you type HELP. Since you have a continue statement right after the function call show_help() program the will go back to executing the while statement again without ever executing the function add_to_list(). You should therefore write your code like this:

if new_item == 'DONE': break elif new_item == 'HELP': show_help() continue add_to_list(new_item)