Python Forum
Need help with simple shopping cart
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with simple shopping cart
#1
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()

Buran - thank you and noted.
Reply
#2
You're deleting from the shopping cart that you loaded from the file. That's not the same as deleting from the file. They're two separate entities. You need to rewrite the file based on the shopping card with the deleted item.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
So you now write to the file only when the user chooses choice==2. Instead, write the file when the user exits, choice ==4. Also, shoppinglist() calls itself which can lead to a recursion error. This example uses a while instead
##---------- Not Tested i.e. it is your homework

import pprint

## In the beginning, the file only contains "Shopping list:
## so no reason to write this at this time
##fp = open('/Users/dsmith/Shopping list.txt', 'w')
##fp.write("Shopping list\n")
##fp.close()
 
shoppinglistfile_list=["Shopping list"]

print()
 
def shoppinglist():
  while True:
    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:
        pprint.pprint(shoppinglistfile_list)
        print()

    elif choice == 2:
        thing_to_add = input("What would you like to add to your shopping list? ")
        shoppinglistfile_list.append(thing_to_add)
        print()
 
    elif choice == 3:
        print()
        pprint.pprint(shoppinglistfile_list)
        del_item = input("Enter item to delete ")
        print(del_item)
        shoppinglistfile_list.remove(del_item)
        print()
 
    elif choice == 4:
        print()
        print("Thank you for using ths program")
        print("--------")

        ## Python already uses the name "file"
        fp=open('/Users/dsmith/Shopping list.txt', "w")
        for rec in shoppinglistfile_list:
            fp.write("%s \n" % (rec))
        fp.close()

        return  ## exits function

    else:
        print()
        print("Please enter a valid option")
        print()
 
 
shoppinglist()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (New to Python) Shopping List Code Farzan1 3 2,606 Oct-28-2019, 04:36 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020