Python Python Basics (2015) Shopping List App Shopping List Introduction

Shopping list application error.

Hi all,

I'm currently stumped with the shopping list application whereby when I output my final list it only outputs the word 'DONE' instead of the items that have been added.

I've been following Kenneth's tutorial with much attention but cannot determine where the error is coming from. Here is my code:

 shopping_list = []

print("Welcome to the shopping app!")
print("To use it, simply add items to the list.")
print("To quit, simply type 'DONE' in all uppercase")
print("What would you like to enter?")

while True:
    new_item = input("Item: ")

    if new_item == 'DONE':
        break

shopping_list.append(new_item)

print("Here are your items: ")

for item in shopping_list:
    print(item)

Moderator edited: Added markdown so the code renders properly in the forums.

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! The problem lies in the placement of the line that adds the item to the shopping list. It is outside of your while loop which means it will only ever be run once. At the time it runs, new_item is equal to "DONE" which also means that the only item to be added to your list will be "DONE" and that is what printed. But if we just indent one line, it works like a charm! Take a look:

shopping_list = []

print("Welcome to the shopping app!")
print("To use it, simply add items to the list.")
print("To quit, simply type 'DONE' in all uppercase")
print("What would you like to enter?")

while True:
    new_item = input("Item: ")  #Ask the user for an item

    if new_item == 'DONE':  #If the item is "DONE" exit the loop
        break

    #Note that indenting this line puts it inside the loop as opposed to outside the loop
    shopping_list.append(new_item)  #If the item was not "DONE" add the item to the list and run the loop again


print("Here are your items: ")

for item in shopping_list:
    print(item)

Hope this helps! :sparkles:

Aha! Brilliant! thanks a bunch.