购物车

简介:
需求
    要求用户输入总资产,例如:2000
    显示商品列表,让用户根据序号选择商品,加入购物车
    购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    附加:可充值、某商品移除购物车
测试信息
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

wKioL1loNCGwGJuyAADhuUj-eIM573.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
goods  =  [
     { "name" "电脑" "price" 1999 },
     { "name" "鼠标" "price" 10 },
     { "name" "游艇" "price" 20 },
     { "name" "美女" "price" 998 },
]
# 定义函数,格式化菜单输出
def  menu():
     count = 0
     for  in  goods:
         print ( '【{0}】    {1}     {2}' . format (count,i[ 'name' ],i[ 'price' ]))
         count + = 1
# 定义购物车空列表
goods_list = []
tag = True
while  tag:
     money = input ( 'How much are you ?' ).strip()
     # 判断输入的数据类型
     if  money.isdigit()  and  len (money) ! =  0  :
         money  =  int (money)
         while  tag:
             menu()
             choice = input ( '请输入你要购买的商品序号:' ).strip()
             if  choice.isdigit()  and  len (choice) ! =  0  :
                 choice = int (choice)
             if  choice > =  0  and  choice < =  3 :
                 # 取出所买商品的信息
                 g_l = []
                 for  in  goods:
                     g_l.append({i[ 'name' ]: i[ 'price' ]})
                 info = g_l[choice]
                 price_list = list (info.values())
                 price = int (price_list[ 0 ])
                 name_list = list (info.keys())
                 # 使用a的值来控制下面两层while循环
                 a = 1
                 while  = =  1 :
                     if  money > =  price:
                         money = money  -  price
                         print ( '商品已加入购物车,当前余额为{}' . format (money))
                         goods_list.append(name_list[ 0 ])
                         while  = =  1 :
                             print ( '当前购物车里商品:' ,goods_list)
                             choices = input ( '''
【E】     结账
【D】     删除商品
【S】     继续购买
  请选择:''' ).strip().lower()
                             if  choices ! =  'e'  and  choices ! =  'd'  and  choices ! =  's' :
                                 print ( '选择错误,请重新输入:' )
                                 continue
                             if  choices  = =  's' :
                                 a = 0
                                 break
                             if  choices  = =  'e' :
                                 print ( '已结账,当前余额为{}' . format (money))
                                 a = 0
                                 tag = False
                             if  choices  = =  'd' :
                                 print (goods_list)
                                 delet = input ( '请输入你想删除的商品:' ).strip()
                                 if  delet  in  goods_list:
                                     goods_list.remove(delet)
                                     print ( '%s 删除成功!'  % delet)
                                     continue
                                 else :
                                     print ( '此商品不在购物车' )
                                 continue
 
                     else :
                         print ( '你买不起这个,请选择:' )
                         choicess = input ( '''
【A】     重新选择商品
【B】     充值
【C】     退出
请选择:''' ).strip().lower()
                         if  choicess  = =  'a' :
                             a = 0
                         if  choicess  = =  'c' :
                             exit()
                         while  = =  1 :
                             if  choicess  = =  'b' :
                                 recharge = input ( '请输入充值金额:' ).strip()
                                 if  recharge.isdigit()  and  len (recharge) ! =  0 :
                                     recharge  =  int (recharge)
                                     money  =  money  +  recharge
                                     print ( '充值成功,当前金额为:' ,money)
                                     break
                                 else :
                                     print ( '充值金额有误' )
                                     continue
             else :
                 print ( '没有此商品,请重新选择' )
                 continue
     else :
         print ( '你的输入有误,请输入数字:' )
         continue
本文转自lyndon博客51CTO博客,原文链接http://blog.51cto.com/lyndon/1947432如需转载请自行联系原作者                                                                                                                                                                            迟到的栋子
相关文章
|
Java API 开发工具
如何用阿里云 oss 下载文件
阿里云对象存储服务(OSS)提供了多种方式下载文件,以下讲解下各种方式的下载方法
9651 2
|
Cloud Native 数据挖掘 Linux
Alibaba Cloud Linux和CentOS有什么区别?
阿里云服务器Linux操作系统Alibaba Cloud Linux和CentOS有什么区别?
2781 0
Alibaba Cloud Linux和CentOS有什么区别?
|
弹性计算
阿里云免费企业邮箱申请流程
免费版的阿里云企业邮箱给你一个很好的体验,享受中小企业发展时期的优惠扶持。那怎么才能申请阿里云免费的企业邮箱呢??
43418 0
|
SQL 存储 前端开发
显示购物车列表【项目 商城】
显示购物车列表【项目 商城】
212 0
云大使推广奖励—云气值提现规则(包含企业/个人)
云大使推广奖励计—云气值是阿里云大使计划中的积分单位,可用于兑换现金和其他权益,100云气等值于1元人民币。个人与企业用户均可参与云大使推广,但提现流程有所不同。个人用户需绑定支付宝并扣税,而企业用户需完善账户信息并绑定对公账户,上传符合要求的发票后方可提现且抵税。更多详情及规则,请访问云大使官网及管理平台获取。
|
10月前
|
弹性计算 运维 对象存储
如何把阿里云ECS里的文件下载到本地(免登录免配置)
阿里云推出新功能,无需安装额外工具、不需ECS开通公网或登录ECS,仅通过控制台即可将ECS中的文件轻松下载至本地。只需开通OSS对象存储服务,按步骤创建任务,获取临时下载链接,即可在浏览器中直接下载文件。操作简便快捷,适合频繁需要从ECS下载文件的用户。
|
12月前
|
关系型数据库 MySQL Linux
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
2168 3
|
对象存储 数据库
2025年 | 10月云大使推广奖励规则
云大使推广返利活动,企业新用户下单返佣加码5%,推广最高返佣45%,新老用户都可参与返利活动。
98240 52
|
人工智能 自然语言处理 API
阿里云百炼平台上线首个最新文生图模型FLUX中文优化版
由Stable Diffusion团队推出的开源文生图模型FLUX风靡全球,其生成质量媲美Midjourney,被誉为“开源王者”。阿里云百炼平台首发FLUX中文优化版,提升了中文指令的理解与执行能力。开发者可直接在平台上体验并调用FLUX模型,且享有1000张图像的免费生成额度,有效期180天。无需额外部署,即可轻松利用这一先进模型创造高质量图像。
1719 0
|
运维 安全 数据建模
阿里云免费SSL证书与付费SSL证书区别及免费SSL证书申请教程参考
免费SSL证书是阿里云为个人网站、开发测试或企业网站建设之初提供的SSL证书,以便满足其对HTTPS数据加密传输的基本要求,从而提高数据传输的安全性。本文为大家详细介绍阿里云免费SSL证书与付费SSL证书区别以及免费SSL证书申请教程。
阿里云免费SSL证书与付费SSL证书区别及免费SSL证书申请教程参考