I'm working on simple shopping cart code that reads and writes from a text file call Shopping Cart.txt.
I'm able to write to the file but not able to delete from it. The code doesn't generate any error while deleting but doesn't delete neither. Can someone point me to a right direction?
Here's the code:
file = open('/Users/dsmith/Shopping list.txt', 'w')
file.write("Shopping list\n")
file.close()
print("Welcome to the shopping list creator")
print()
def shoppinglist():
print('''Choose 1 to view your shopping list.
Choose 2 to add an item to your shopping list.
Choose 3 to delete an item from your shopping list.
Choose 4 to exit the program.''')
print()
choice = int(input("Enter your choice: "))
if choice == 1:
shoppinglist_file = open('/Users/dsmith/Shopping list.txt')
print()
print(shoppinglist_file.read())
print()
shoppinglist_file.close()
shoppinglist()
elif choice == 2:
shoppinglist_file = open('/Users/dsmith/Shopping list.txt', 'a')
print()
thing_to_add = str(input("What would you like to add to your shopping list? "))
shoppinglist_file.write("%s" % (thing_to_add))
shoppinglist_file.close()
print()
shoppinglist()
elif choice == 3:
shoppinglist_file = open('/Users/dsmith/Shopping list.txt')
shoppinglistfile_list = shoppinglist_file.readlines()
print()
print(shoppinglistfile_list)
del_item = str(input)
print(del_item)
shoppinglistfile_list.remove(del_item)
print()
shoppinglist()
elif choice == 4:
print()
print("Thank you for using ths program")
print("--------")
else:
print()
print("Please enter a valid option")
print()
shoppinglist()
shoppinglist()