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 Introducing Lists Build an Application Display the List

ARUN DAMODARAN
ARUN DAMODARAN
733 Points

Shopping_List.py

I would like to spice up this code a little bit by adding a new function named as remove_list() to the existing code. The objective is to ask the user to provide with an input to delete items from the added list.

Below is how I wrote the code and it the DEL operation works but the problem is as the user inputs "DEL" keyword it adds to the existing list which should not be case as "DEL" is supposed to work as a caption to perform the operation.

Can anyone please advise what am I doing wrong:

shopping_list = []

def show_help(): print("What should we pock up at the store?") print(""" Enter 'DONE' to stop adding items. Enter 'HELP' for this help. Enter 'SHOW' to see your current list. """)

def add_to_list(item): shopping_list.append(item) print("Added! List has items.".format(len(shopping_list)))

def remove_list(item): shopping_list.remove(item) print("Deleted! The Item Deleted was. {}".format(item))

def show_list(): print("Here's your list:") count = 1 for item in shopping_list: print("{}. {}".format(count,item)) count +=1

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

if new_item == "DONE":
    break
elif new_item =="HELP":
    show_help()
    continue
elif new_item == "SHOW":
    show_list()
    continue

add_to_list(new_item)

if new_item == 'DEL':
    rem_item = input("Enter the Item you wish to Delete:")
    remove_list(rem_item)

show_list()

Adam Bonny
Adam Bonny
2,860 Points

To solve the problem of 'DEL' being added to the list you need to add 'continue' right after remove_item(rem_item).

A couple other changes that I would suggest would be the following ones:

if new_item == 'DEL':
    rem_item = int(input("Enter the Item you wish to Delete:")) - 1
    remove_list(rem_item)
    continue

I would change the input into an integer with int() and then remove one since python uses a 0-based index so if the user wants to delete what he thinks is item 1 you need to subtract 1 to actually remove the correct item.

Hope it helps :)

2 Answers

Josh Keenan
Josh Keenan
20,315 Points

Because you have a separate if statement for it, you need to add it as an elif to the other clauses above; at present your code does this:

# evaluate these and if one of these three options, do what's needed
if new_item == "DONE":
    break
elif new_item =="HELP":
    show_help()
    continue
elif new_item == "SHOW":
    show_list()
    continue

# YOU ADD WHATEVER IT IS TO THE LIST HERE
add_to_list(new_item)

# and after you check if it is to be deleted or not
# so move this above the segment above!
if new_item == 'DEL':
    rem_item = input("Enter the Item you wish to Delete:")
    remove_list(rem_item)

Hope this helps!

ARUN DAMODARAN
ARUN DAMODARAN
733 Points

Hello Josh, Thanks for your response.

Below is what I did and it worked for me.

elif new_item == "DEL": rem_item = input("Enterthe Item you wish to Delete:") remove_list(rem_item) continue

Josh Keenan
Josh Keenan
20,315 Points

Glad you managed to solve it! You can do it!