-1

My code works with adding and deleting one item only. However, when I add multiple items, I'm unable to delete any of them. I think the issue is that my code reads entire string (all of items including spaces) as one single item. How do I separate them in the cart?

file = open('/Users/home/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 veiw 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/home/Shopping list.txt')
    print()
    print(shoppinglist_file.read())
    print()
    shoppinglist_file.close()
    shoppinglist()

elif choice == 2:
    shoppinglist_file = open('/Users/home/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/home/Shopping list.txt')
    shoppinglistfile_list = shoppinglist_file.readlines()
    print()
    print(shoppinglistfile_list)
    del_item = str(input())
    print(del_item)
    shoppinglistfile_list.remove(del_item)
    shoppinglist_file.close()
    shoppinglist_file = open('/Users/home/Shopping list.txt', 'w')

    shoppinglist_file.write(str(shoppinglistfile_list))
    shoppinglist_file.close()
    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()
fractals
  • 816
  • 8
  • 23
  • Possible duplicate of [Writing string to a file on a new line every time](https://stackoverflow.com/questions/2918362/writing-string-to-a-file-on-a-new-line-every-time) – MGP Aug 28 '18 at 04:30

2 Answers2

0

You're not writing a line break to signal the end of a line.

Try adding \n like this:

shoppinglist_file.write("%s\n" % (thing_to_add))
MGP
  • 2,981
  • 35
  • 34
  • When I try this approach, I'm unable to delete an entry form the shopping cart is it cannot be found: ValueError: list.remove(x): x not in list – Dennis Callagher Aug 28 '18 at 22:56
0

First of all, like MGP mentioned, you are not adding a line break. You first need to fix that to

shoppinglist_file.write("%s\n" % (thing_to_add))

Now as you can see when print(shoppinglistfile_list) is run, readlines() method does not trim any new line characters. So for example, if your Shopping List.txt looks like:

apples\n
oranges\n
bananas\n

(new line character written explicitly for explanation)

then your shoppinglistfile_list would be ['apples\n', 'oranges\n', 'bananas\n'].

input() however treats the new line character as a delimiter, which means you cannot possibly pass \n using it. For the same example, therefore, typing in apples will not remove apples\n. This is easy to fix: simply add \n.

shoppinglistfile_list.remove(del_item + '\n')

You also might want to change how the shopping list is printed, as ['apples\n', 'oranges\n', 'bananas\n'] isn't the prettiest (to me at least).

fractals
  • 816
  • 8
  • 23