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 trialKyle Hille
720 Pointsshopping list deleting an item from the list, how to? Python
So there is a video that Kenneth Love teaches about creating a shopping list. I wanted to add onto it, just for my own learning sake and add the ability to delete an item from the list.
The problem is, that it never deletes it haha. I'm sure I have done something simple, or maybe I'm just crazy and wrong, but here is what I have :
shopping_list = []
def show_help():
print("What should we pick up at the store?")
print("""
Enter 'DONE' to stop adding items.
Enter 'SHOW' to show your current list.
Enter 'DELE' to delete an item from the list.
Enter 'HELP' to get back to this message.
""")
def show_list():
print("Here is your list :")
for item in shopping_list:
print(item)
def add_to_list(new_item):
shopping_list.append(new_item)
print("Added {}. List now has {} items.".format(new_item, len(shopping_list)))
#created a new function
def delete_from_list():
#get input from user to figure out what they want to remove
delete_me = input("Which item would you like to delete from the list? ")
#remove that item from the list
shopping_list.remove(delete_me)
#written confirmation showing that item has been removed from the list
print("{} has been removed from the list.".format(delete_me))
show_help()
while True:
new_item = input("> ")
if new_item == 'DONE':
break
elif new_item == 'DELE':
#shows the list, so you don't have to try to delete from memory
show_list()
#run delete_from_list function
delete_from_list()
continue
elif new_item == 'HELP':
show_help()
continue
elif new_item == 'SHOW':
show_list()
continue
add_to_list(new_item)
show_list()
1 Answer
Osher Jacobs
2,226 PointsIn answer to your first and second question: the code runs fine (as in the items get removed) if you change the indentation of the print command:
"print("{} has been removed from the list.".format(delete_me))"
to match the indentation of the list method call "shopping_list.remove(delete_me)"
In your modified code the indentation is the same. I think that was the issue.
Hope this helps.
Kyle Hille
720 PointsAh yes! I must have fixed that naturally when I was going back through and adding more. Cheers mate!
Kyle Hille
720 PointsKyle Hille
720 PointsAnd three minutes later, while waiting for an answer, I think to myself : It would be nice to know how many items you have left in your list after you remove one, so you don't have to type in another item, just to remove it again, or type SHOW to have a full count. So I copy pasted from the add_to_list function and now it works? I'm not really sure what caused this? I did not change anything from the elif == 'DONE' call.