Item 1: Simulated shopping cart


The project code is written by myself, without any plagiarism or reprinting, so it is very low, just for your reference.

Project requirements:

  1. Users first charge their accounts: for example, 3000 yuan.

  2. There is a format as follows:

    goods = [
     {"name": "Computer", "price": 1999},
        {"name": "mouse", "price": 10}, 
        {"name": "Yacht", "price": 20},
        {"name": "Beauty", "price": 998}, 
  3. Page display serial number + commodity name + commodity price, such as: 1 computer 1999

  4. The user enters the selected serial number of the goods, then prints the name and price of the goods, and adds the goods to the shopping cart (self-defined shopping cart), and the user can continue to add the goods.

  5. If the serial number of the product entered by the user is incorrect, the user is prompted for incorrect input and re-input.

  6. Users enter N for shopping cart settlement, showing the goods, quantity and unit price in the shopping cart in turn. If the amount of recharged money is insufficient, the user can delete a certain commodity until it can be purchased. If the amount of recharged money is sufficient, the user can purchase it directly.

  7. The user enters Q or Q to exit the program.

  8. After withdrawing from the program, display the goods, quantity, unit price, and the total amount of money spent, the balance of the account, and display the purchase information.

Source code:

goods = [{"name": "Computer", "price": 1999},
         {"name": "mouse", "price": 10},
         {"name": "Yacht", "price": 20},
         {"name": "Beauty", "price": 998}, ]
shopping_trolley = []  # Shopping Cart
bill = []  # Settlement bill
print("Welcome to Mall".center(18, "*"))
while True:
    money = input("Please recharge the amount:")
    if money.isdigit() and int(money) > 0:  #  Must be an integer greater than 0
        print(f"The amount of your recharge is:{money}element")
        print("Commodity information".center(18, "*"))
        for i, el in enumerate(goods, 1):  # Enumeration Unwrapping Sequence Number
            value_lst = list(el.values())
            print(f"Serial number:{i} commodity:{value_lst[0]} Price:{value_lst[1]}")
        while True:
            number = input("Please enter the serial number of the goods you want to buy.(Press N Settlement, press Q Sign out):")
            if number.upper() != "Q":
                if number.isdigit() and 0 < int(number) < len(goods) + 1:
                    number = int(number)
                    commodity = (number, goods[number - 1]["name"], goods[number - 1]["price"])  # Packing of selected items
                    shopping_trolley.append(commodity)  # Add to the shopping cart
                    print(f"commodity:{goods[number - 1]['name']} Price:{goods[number - 1]['price']}")  # Format the output of purchased goods
                elif number.upper() == "N":  # Settlement
                    if shopping_trolley == []:
                        print("There is no merchandise in the shopping cart, please re-select the merchandise!")
                        print("\n"+"You have selected the following items".center(18, "*"))
                        price_sum = 0  # Total Price Initialization
                        remove_repeat = set(shopping_trolley)  # Heavy shopping cart
                        for el in remove_repeat:
                            time = shopping_trolley.count(el)  # Calculate the number of times duplicates occur
                            num, name, price = el  # Unpack tuple
                            print(f"commodity:{name} Price:{price} Number:{time}")
                            price_sum = price_sum + price * time  # Calculated price
                        while True:
                            if price_sum > int(money):
                                print("\n Your recharge amount is insufficient, please choose to delete the goods in the shopping cart!")
                                print("\n"+"Shopping Cart".center(18, "*"))
                                choose_num = []
                                remove_repeat = set(shopping_trolley)  # Repeat the code to show what's in the shopping cart again
                                for el in remove_repeat:
                                    time = shopping_trolley.count(el)
                                    num, name, price = el
                                    choose_num.append(num)  # Add the serial numbers of the goods in the shopping cart and use them when deleting them later.
                                    print(f"Serial number:{num} commodity:{name} Price:{price} Number:{time}")  # Show the shopping cart again
                                number = input("Please enter the serial number of the item you want to delete.:")
                                if number.isdigit() and int(number) in choose_num:  # Determine if the input serial number is not in the shopping cart
                                    if len(shopping_trolley) > 1:
                                        for i in range(len(shopping_trolley)):
                                            if shopping_trolley[i][0] == int(number):
                                                price_sum = price_sum - shopping_trolley[i][2]
                                                shopping_trolley.pop(i)  # delete
                                                break  # Only one deletion at a time, so there is no problem that circular list deletion is not clean.
                                    else:  # At this point, there's only one item left in the shopping cart.
                                        shopping_trolley.clear()  # Just empty it directly.
                                        print("There is no merchandise in the shopping cart!")
                                        price_sum = 0  # Total commodity price cleared
                                    print("The serial number you entered is incorrect. Please re-enter it!")
                                choose = input("You have enough money to confirm whether you want to buy it or not.(yes/no)?")
                                if choose == "yes":
                                    print("Successful purchase!")
                                    for el in shopping_trolley:
                                        bill.append(el)  # Add the settled goods to the bill
                                    shopping_trolley.clear()  # empty cart
                                    price_sum = 0  # Total commodity price returns to zero
                                elif choose == "no":
                                    print("The shopping cart has been emptied!")
                                    print("Your input is incorrect, please re-enter!")
                    print("The serial number you entered is incorrect. Please re-enter it!")
                print("Settlement list".center(18, "*"))
                print("The items you purchased are as follows.:")
                price_sum = 0
                remove_repeat = set(bill)  # The bill goes heavy.
                for el in remove_repeat:
                    time = bill.count(el)  # Calculate the number of duplicates in the bill
                    num, name, price = el
                    print(f"commodity:{name} Number:{time} Price:{price}")
                    price_sum = price_sum + price * time
                money = int(money) - price_sum  # Surplus amount
                print(f"Total Consumption:{price_sum}element\n The balance of the account is:{money}element\n Your shopping cart has been emptied. Welcome to visit next time!")
        print("Input amount is incorrect, please re-enter!")