python实战小程序之购物车
2024-10-19 13:38:34
# Author:南邮吴亦凡
# 商品列表
product_list = [
('Iphone',5800), # 逗号一定不可以省略!
('Mac',4800),
('smartphone',400),
('watch',9100),
('coffee',500),
('python',9200),
]
shopping_list = []
# 工资
salary = input("Input your salary:") # 输入的是字符串
if salary.isdigit(): # 如果这是一个数字,就转成数字
salary = int(salary)
while True:
# for item in product_list:
print(item) # 无序号
# for item in product_list:
print(product_list.index(item),item) # 有序号(1)
for index,item in enumerate(product_list):
print(index,item) # 有序号(2)
# 选择买的商品
user_choice = input("选择要买吗?>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice <len(product_list) and user_choice >=0:
p_item = product_list[user_choice]
if p_item[1] <=salary: # 买得起
shopping_list.append(p_item)
salary -= p_item[1] # 把商品的钱从工资里扣除
print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary))
# print("Added %s into shopping cart,your current balance is %s" %(p_item,salary)) # 余额没有颜色
else: # 买不起
print("\033[41;1m你的余额只剩[%s]啦\033[0m" % salary) # 31无背景的红色,41有背景的红色
print("商品[%s]不存在")
elif user_choice =='q':
print("-----shopping list------")
for p in shopping_list:
print(p)
print("your current balance:",salary)
exit()
else:
print("invalid option")
最新文章
- BZOJ 1260&;UVa 4394 区间DP
- JDBC/PreparedStatement
- 点击空白处div消失的方法
- Angular2 CLI 快速开发
- C#操作XML方法集合
- SVM支持向量机的高维映射与核函数-记录毕业论文2
- hdu-acm steps Max sum
- POJ 3169 Layout (差分约束系统)
- lucene4.0 基于smb文件服务器的全文检索
- verilog中阻塞复制,非阻塞复制,顺序块,并行块之间的关系
- scipy cluster聚类 ---Python3
- container_of 的用法
- Qt全局宏和变量
- 我们一起来排序——使用Java语言优雅地实现常用排序算法
- (原创)cocos2dx-lua TableView官方demo分析
- Netty权威指南(笔记一)
- [UE4]UMG小结
- web socket
- [转]Linux下安装Java环境配置步骤详述
- python学习笔记_week4
热门文章
- 关于我在17号“一个查询任意年份中任意月份的天数”程序编写中的代码&;第二种方法!
- 使用liner、feather、multiband对已经拼接的数据进行融合(下)
- Django文件存储(二)定制存储系统
- JavaScript中数组中的方法:push()、pop()、shift()、unshift()、slice()、splice()、reverse()、join()、split()、concat()、indexOf()、forEach()、map()、
- smbclient和mount -t cifs共享win的共享文件夹? autocad小记
- 如何卸载旧版本的dotnet core
- 【htop】Linux CentOS 6.5下安装htop进程管理工具
- sed 替换换行回车
- MPI之聚合通信-Scatter,Gather,Allgather
- 常用的 Linux 命令