python writes shopping cart

Last time, I didn't persist in my study, and I was busy with my work, but I reflected that I didn't make up my mind to study hard, so this three-month study plan started. Here is the first version of the shopping cart code that was rewritten after this study.

 1 # Functional requirements:
 2 # Users are required to input total assets, for example: 2000
 3 # Display the list of products, let the user select the products and add them to the shopping cart
 4 # Purchase, if the total amount of goods is greater than the total assets, the account balance is insufficient, otherwise, the purchase is successful.
 5 # Add: rechargeable, goods removal shopping cart
 6 goods = [
 7     {"name": "Computer", "price": 1999},
 8     {"name": "mouse", "price": 10},
 9     {"name": "Yacht", "price": 20},
10     {"name": "Beauty", "price": 998},
11 ]
12 zongxiaofei = 0
13 gouwuche = {}
14 zongzichan = int(input("Welcome to the small store, please input your total assets:"))
15 print("Your total assets are:", zongzichan, "\n The commodity name and price are as follows:")
16 for shangpin_list in goods:
17     print(shangpin_list["name"], shangpin_list["price"])
18 print("\n")
19 while True:
20     goumai = input("Please input'Trade name'Add to cart or enter'y'or'Y'Go to the shopping cart to settle:")
21     if goumai.lower() == "y":
22         break
23     for shangpin_list2 in goods:
24         if shangpin_list2["name"] == goumai:
25             if  shangpin_list2["name"] not in gouwuche.keys():
26                 gouwuche[shangpin_list2["name"]] = {"num": 1, "jiage": shangpin_list2["price"]}
27             else:
28                 gouwuche[shangpin_list2["name"]]["num"] = gouwuche[shangpin_list2["name"]]["num"] + 1
29 for jiesuan_key , jiesuan_values in gouwuche.items():
30     jiesuan_num = jiesuan_values["num"]
31     jiesuan_jiage = jiesuan_values["jiage"]
32     xiaofei = jiesuan_num * jiesuan_jiage
33     zongxiaofei = zongxiaofei + xiaofei
34 while True:
35     if zongxiaofei > zongzichan:
36         print("Purchase failed!")
37         print("Your total assets are:", zongzichan, "Your total consumption amount:", zongxiaofei, "Excess amount:", zongxiaofei - zongzichan)
38         chongzhi = input("Please input'y'or'Y'Go to recharge or input'n'or'N'Go back to the cart and delete extra items:")
39         while True:
40             if chongzhi.lower() == "y":
41                 chongzhi_money = int(input("Please enter the recharge amount:"))
42                 zongzichan = zongzichan + chongzhi_money
43                 print("Your total assets are:", zongzichan, "\n")
44                 break
45             if chongzhi.lower() == "n":
46                 print("\n The name, quantity and unit price of the consumer goods in your shopping cart are as follows:")
47                 for jiesuan_key2, jiesuan_values2 in gouwuche.items():
48                     print(jiesuan_key2, jiesuan_values2["num"], jiesuan_values2["jiage"])
49                 shanchu = input("\n Please enter the product name to reduce the number of products 1:")
50                 for jiesuan_key3 , jiesuan_values3 in gouwuche.items():
51                     if jiesuan_key3 == shanchu:
52                         if jiesuan_key3 in gouwuche.keys():
53                             gouwuche[jiesuan_key3]["num"] = gouwuche[jiesuan_key3]["num"] - 1
54                             zongxiaofei = zongxiaofei - gouwuche[jiesuan_key3]["jiage"]
55                 break
56     else:
57         print("Purchase succeeded!")
58         print("Your total assets are:", zongzichan, "Your total consumption amount:", zongxiaofei, "Balance:", zongzichan - zongxiaofei)
59         exit()

This version is to add value, delete shopping cart goods, and finally settle the amount.

Later, I will learn to simplify the code and write another version to judge the balance in real time.

Tags: ASP.NET

Posted on Fri, 01 Nov 2019 05:30:51 -0700 by Fruct0se