python购物车程序详解用字典_Python_购物车程序[列表嵌套字典]

1 product = [{"名称": "电脑", "价格": 999, '数量': 0}, {"名称": "硬盘", "价格": 320, '数量': 0}, {"名称": "内存",2 "价格": 429, '数量': 0}, {"名称": '主板', "价格": 699, '数量': 0}]3 print("-----------------------------------\n序号\t\t名称\t\t价格")4 for index, dic in enumerate(product, 1):5 print(index, "\t\t\t" + dic.get("名称"), "\t\t" + str(dic.get("价格")) + "")6 print("n 购 物 车 结 算")7 print("-----------------------------------")8 money = input("\033[36m请输入充值金额:\033[0m").strip()9 whileTrue:10 ifstr.isdigit(money):11 money = int(money)12 print("\033[34m您充值的金额为:¥{:.2f}\033[0m".format(money))13 break

14 else:15 money = input("\033[36m输入错误,请重输入充值金额:\033[0m").strip()16 whileTrue:17 choice = input("\033[36m\n1.请输入你要购买的商品序号 2. 按 N 键 查看购物车(结算)>>>\033[0m").strip()18 # ---------------------------------------------- 商 品 购 买 环 节 ------------------------------------------------------

19 ifstr.isdigit(choice):20 if 0 < int(choice) <=len(product):21 choice_add = product[int(choice) - 1]22 # choice_add = product[int(choice) - 1] # 读 取 商 品 的 键23 choice_add['数量']=choice_add['数量']+1

24 print("已将下列商品加入购物车" + "\t" + choice_add.get("名称") + "\t" + str(choice_add.get("价格"))\25 + '\t\t'+str(choice_add.get('数量')))26 else:27 print('商品序号不正确,请输入正确的商品序号>>>')28 # ---------------------------------------------------- 商 品 展 示 环 节 ----------------------------------------------

29 elif choice == 'n':30 print('\033[33m您好,您的购物车商品如下:\033[0m')31 def show_shop():32 globalcost33 cost = 0

34 print("---------------------------------\n序号\t名称\t\t价格\t数量")35 for k,i in enumerate(product,1):36 if i.get('数量')>0:37 globalsingle38 print(k, '\t\t'+i.get('名称') + '\t' + '\t' + str(i.get('价格')) + '\t\t' + str(i.get('数量')))39 single = i.get('价格') * i.get('数量')40 cost +=single41 print("---------------------------------")42 show_shop()43 print('\033[33m温馨提示:余额为:%s 购物车:%s\033[0m' %(money, cost))44

45 # ---------------------------------------------------- 商 品 删 除 环 节 ----------------------------------------------

46 whileTrue:47 choice_del_select = input('\033[36m\n1.输入商品序号,进行删除,2.输入 Y 进入进结算程序:\033[0m')48 ifstr.isdigit(choice_del_select):49 if 0 < int(choice_del_select) <=len(product):50 choice_del = product[int(choice_del_select) - 1]51 if choice_del['数量'] > 0:52 choice_del['数量'] = choice_del['数量']-1

53 else:54 print("本商品已清空,请选择已购买的商品")55 else:56 print("您输入的商品未购买,重新选择")57 # ---------------------------------------------------- 商 品 结 算 环 节 -----------------------------------------------

58 elif choice_del_select.strip().upper() == 'Y':59 if money >cost:60 money -=cost61 # print('成功加入购物车 当前余额为:%s' %(money))62 print('\033[34m\n......结 算 成 功,按 Q 按 键 退 出.......\033[0m')63 # show_shop()64 break

65 else:66 money_add = input('\033[36m您的充值金额不足,请输入充值金额进行结算:\033[0m')67 money += int(money_add)68 if money >cost:69 all_money =money70 money -=cost71 # print('\033[34m\n总共充值 ¥{:.2f} 此时总余额为:¥{:.2f}\033[0m'.format(all_money, money))72 print('\033[34m\n......结 算 成 功,按 Q 按 键 退 出.......\033[0m')73 # show_shop()74 break

75 show_shop()76 print('温馨提示:余额为:%s 购物车:%s' %(money, cost))77 # ---------------------------------------------------- 商 品 退 出 环 节 -----------------------------------------------

78 elif choice.strip().upper() == 'Q':79 print('消费成功,购买商品如下')80 show_shop()81 print('\033[36m此次共花费: ¥{:.2f}:余额为:¥ {:.2f}\033[0m'.format(cost, money))82 break

83 else:84 print('输入错误,请重新输入')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值