博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
购物车程序练习
阅读量:4352 次
发布时间:2019-06-07

本文共 1221 字,大约阅读时间需要 4 分钟。

1.开发一个购物车程序

要求:选定商品后需要减少相应的金额并将商品存入购物车

salary=input("卡内金额:")if salary.isdigit():    salary = int(salary)else:    print("请输入正确的数据类型")shop_list=[    ('苹果5S',4999),    ('美的空调',3000),    ('联想笔记本',6890),    ('小米电源',210),    ('乐视TV',5999),    ('华为荣耀8',3999),    ('荣耀7',2000),    ('苹果7PLUS',7000)]shop_car=[]tag=Falsewhile tag is not True:    #当循环遇到tag为True时停止循环    print("商品清单列表".center(50,'-'))    for item in enumerate(shop_list):        index=item[0]        i_name=item[1][0]        i_price=item[1][1]        print(index,i_name,i_price)    user_select=input("[q=quit,c=check] 输入你要购买的商品编号:")    if user_select.isdigit():         user_select = int(user_select)         if user_select < len(shop_list):             p_item=shop_list[user_select]             if p_item[1] <= salary:                 shop_car.append(p_item[0])                 salary -= p_item[1]                 print("购买了商品\033[41;1m%s\033[0m,剩余金额\033[32;1m %s\033[0m"%(p_item[0],salary))         else:             print("余额不足")    elif user_select == 'c' or user_select == 'check':        print("加入购物车的商品".center(50,'*'))        print(shop_car)    else:        print("不买了")        tag=True
View Code

 

测试运行:
 

转载于:https://www.cnblogs.com/shansongxian/p/6074590.html

你可能感兴趣的文章
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 角色权限的配置页面改进优化...
查看>>
如何编写Spring-Boot自动配置
查看>>
(三)Asp.net web api中的坑-【http post请求中的参数】
查看>>
洛谷跑路
查看>>
使用DbProviderFactories.GetFactory方法需要配置数据库提供者
查看>>
Ubuntu || LinuxMint 配置apache虚拟主机
查看>>
HTML—链接
查看>>
将进程设置为守护进程
查看>>
用连接池提高Servlet访问数据库的效率
查看>>
luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )
查看>>
树的数据结构
查看>>
MyEclipse导入Color Theme
查看>>
Vue开发微信H5 微信分享签名失败问题解决方案
查看>>
Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法
查看>>
Python(2.7.6) glob - 匹配指定模式的文件
查看>>
HTTP - 持久连接
查看>>
添加路由时啥时候是dev啥时候是gw
查看>>
redis 中文字符显示
查看>>
登录日志分析常用查询
查看>>
Codeforces Round #228 (Div. 1) 388B Fox and Minimal path
查看>>