Python实战购物车项目的实现参考
作者:村雨1943 时间:2021-09-28 11:06:29
购物车程序
要求如下图
代码
# --*--coding:utf-8--*--
# Author: 村雨
import pprint
productList = [('Iphone 8', 10000),
('GTX2080', 8000),
('Z7KP7-GT', 6000),
('Mac pro', 15000),
('Honor 10', 2800),
('Iphone XR', 12000),
('Mi 8', 2999)
]
shoppingList = []
print('输入你的工资:')
salary = input()
if not salary.isdigit():
print('请输入整数')
else:
salary = int(salary)
while True:
for index, item in enumerate(productList):
print(index + 1, item)
print('输入你要买的商品的序号:')
userWant = input()
if userWant.isdigit():
userWant = int(userWant)
if userWant <= len(productList) and userWant > 0:
print('你要购买的是:', productList[userWant - 1][0])
if salary >= productList[userWant - 1][1]:
shoppingList.append(productList[userWant - 1][0])
salary -= productList[userWant - 1][1]
print('你已经购买了' + productList[userWant - 1][0] + ', 你的余额为 ' + str(salary))
else:
print('对不起,你的余额不足!请努力工作吧!')
print('你当前所购买的商品为:')
for brought in shoppingList:
pprint.pprint(brought)
print('你当前余额为:', salary)
exit()
else:
print('你输入的商品序号有错,请重新输入')
elif userWant == 'q':
print('-----------Shopping List----------')
for brought in shoppingList:
pprint.pprint(brought)
print('你的余额为 ', salary)
exit()
else:
print('Invalid input!!!')
结果
来源:https://blog.csdn.net/github_39655029/article/details/82747768
标签:python,购物车
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
编写一个JS组件来说说call和apply的用法
2008-11-23 17:11:00
Chrome和firefox使用比较测评
2010-03-26 12:13:00
![](https://img.aspxhome.com/file/UploadPic/20103/26/2010326124036780s.gif)
python的字典和集合你了解吗
2022-12-13 11:28:44
![](https://img.aspxhome.com/file/2023/0/65550_0s.png)
python time时间库详解
2023-10-09 03:20:57
![](https://img.aspxhome.com/file/2023/4/70754_0s.png)
在Win 2003中配置ASP.net环境
2007-10-14 12:02:00
![](https://img.aspxhome.com/file/UploadPic/200710/14/2007101412855924s.jpg)
ASP.NET中使用SQL存储过程的方法
2007-08-24 09:31:00
Python中match语句的详细用法实例
2023-06-29 11:28:46
![](https://img.aspxhome.com/file/2023/7/60517_0s.png)
python线程信号量semaphore使用解析
2023-02-08 08:47:24
python使用re模块爬取豆瓣Top250电影
2023-10-11 17:28:38
![](https://img.aspxhome.com/file/2023/0/82650_0s.png)
js倒记时代码,自定义时间改变时间,日期到时事件,日期格式。
2010-08-08 08:51:00
Python利用Xpath选择器爬取京东网商品信息
2021-05-18 09:08:41
![](https://img.aspxhome.com/file/2023/4/79554_0s.jpg)
python使用Image处理图片常用技巧分析
2023-01-17 14:51:38
5招优化MySQL插入方法
2009-04-02 10:49:00
Python入门篇之字符串
2022-01-09 00:31:17
Mini,又个 Javascript 选择器
2009-10-06 14:48:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/medium-36s.jpg)
在Dreamweaver中自动设置网页的水平线颜色
2007-02-03 11:30:00
使用PyQt4 设置TextEdit背景的方法
2021-09-01 14:41:43
ASP中CACHE缓存技术
2010-05-03 10:58:00
javascript设计模式 – 简单工厂模式原理与应用实例分析
2023-08-27 19:53:20
设计能力决定权力
2009-06-16 14:48:00
![](https://img.aspxhome.com/file/UploadPic/20096/16/d-68s.jpg)