1 #!/usr/bin/env python 2 #输出商品列表,用户输入序号,显示用户选中的商品 3 # 商品 li = ["手机", "电脑", '鼠标垫', '游艇'] 4 #"""功能要求: 5 #要求用户输入总资产,例如:2000 6 #显示商品列表,让用户根据序号选择商品,加入购物车 7 #购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 8 #附加:可充值、某商品移除购物车"" 9 #""" 10 goods_list = [ 11 {"name":"电脑","price":1999}, 12 {"name":"鼠标","price":10}, 13 {"name":"游艇","price":20}, 14 {"name":"美女","price":998} 15 ] 16 shopping_list = [] 17 for i,j in enumerate(goods_list,1) : 18 print(i,j["name"],j["price"]) 19 total_assets = input("请输入你的总资产") 20 total_money = int(total_assets) 21 while True : 22 inp = input("请输入购买商品的序号,结束购买请按0,查看购物车请按9") 23 24 if int(inp) == 1 : 25 shopping_list.append(goods_list[int(inp) - 1]) 26 27 elif int(inp) == 2 : 28 shopping_list.append(goods_list[int(inp) - 1]) 29 30 elif int(inp) == 3 : 31 shopping_list.append(goods_list[int(inp) - 1]) 32 33 elif int(inp) == 4 : 34 shopping_list.append(goods_list[int(inp) - 1]) 35 elif int(inp) == 9 : 36 while True : 37 inp = input("请输入要移除商品的序号,结束请按0") 38 if int(inp) == 1: 39 shopping_list.remove(goods_list[int(inp) - 1]) 40 print(shopping_list) 41 elif int(inp) == 2: 42 shopping_list.remove(goods_list[int(inp) - 1]) 43 print(shopping_list) 44 elif int(inp) == 3: 45 shopping_list.remove(goods_list[int(inp) - 1]) 46 print(shopping_list) 47 elif int(inp) == 4: 48 shopping_list.remove(goods_list[int(inp) - 1]) 49 print(shopping_list) 50 elif int(inp) == 0: 51 break 52 else : 53 print("请输入正确的商品序号") 54 elif int(inp) == 0 : 55 break 56 else : 57 print("请输入正确的商品序号") 58 print(shopping_list) 59 sum1 = 0 60 for i in shopping_list : 61 val = (i['price']) 62 sum = int(val) 63 sum1 = sum1 + sum 64 print("购物总金额:", sum1) 65 while True : 66 if total_money >= sum1 : 67 inp = input("请按 1 确认购买") 68 if int(inp) == 1: 69 total_money = total_money - sum1 70 print("购买成功") 71 print("剩余资产", total_money) 72 sum1 = 0 73 break 74 else : 75 inp = input("金额不足输入 1 进行充值,输入其他取消购买") 76 if int(inp) == 1: 77 inp =input("请输入充值金额") 78 total_money = total_money + int(inp) 79 print("总资产", total_money) 80 continue 81 else : 82 print("取消购买") 83 break