python day2 购物车

一.需求:

要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车

二.流程图:

 1 goods = [
 2     {"name": "电脑", "price": 1999},
 3     {"name": "鼠标", "price": 10},
 4     {"name": "游艇", "price": 20},
 5     {"name": "美女", "price": 998},
 6 ]
 7 shopping_cart=[]
 8 #name=int(input("请输入您的总余额").strip())
 9 while True:
10     while True:
11         name = input("请输入您的总余额").strip()
12         if (name.isdigit() == True) :   #判断用户输入的是不是合法数字 如果输入字符串重新输入
13             name = int(name)
14             break
15         else:
16             print("请输入正确的总额度")
17 
18     while True:
19 
20         print("请选择你需要的操作")
21         print("1.购物  4.充值 5.退出")
22         print("------------------------------------------")
23         chonice_input = input("请输入序号选择你的操作:")
24         while True:
25             if chonice_input == "1":
26                 print("------------------------以下是你要购买的商品-------------------")
27                 for i in goods:
28                     list = {"商品 {name1}:价格 {name2}".format(name1=i["name"], name2=i["price"])}  # 格式化输出列表
29                     list2 = (goods.index(i), list)
30                     print(list2)
31                 print("---------------------------------------------------------------")
32 
33 
34                 number = input("请输入购买产品序号").strip()  # 让用户输入产品序列号
35                 if number.isdigit():
36                     number = int(number)
37                     shopping_cart.append((goods[number]["name"],goods[number]["price"]))
38                     if goods[number]["price"] < name:
39                         print("购买成功 当前余额为 ¥", name - goods[number]["price"])
40                     else:
41                         print("sorry,账户余额不足")
42                         break
43                     print("---------------a.查看购物车 b.返回 c.退出-----------")
44                     ls=input("请输入你操作的序号:").strip()
45 
46                     if ls.isdigit():
47                         ls == int(ls)
48                     if ls == "a" or ls == "A":
49                         while True:
50                             print("你购买的产品:", shopping_cart)
51                             print("购买成功 当前余额为 ¥", name - goods[number]["price"])
52                             print("a.删除购物车 b.返回 c.退出")
53                             total = (name - goods[number]["price"])
54                             delete_input = input("请选择操作的序号:")
55                             if delete_input == "a":
56                                 for delete in shopping_cart:
57                                     print(shopping_cart.index(delete),delete)
58 
59 
60                             elif delete_input == "b":
61                                 break
62                         break
63 
64             if chonice_input == "2":
65                     print(shopping_cart)
66 
67             if chonice_input == "5":
68                 exit(0)
69             if chonice_input == "4":
70                 chongzhi=int(input("请输入充值的额度:"))
71                 name=(chongzhi + name)
72                 break
原文地址:https://www.cnblogs.com/wangshaojie/p/7168654.html