python_day4_shopping
购物车例子,实现显示商品信息,输入商品编号并且可以减去自己的存入余额,当商品价格大于自己的余额的时候,直接退出;当不再选择商品的时候,退出显示余额和已经添加的商品。
#购物车程序 product_list = [
("airplane",90000),
("pen", 80),
("Trek bike", 5000),
("Book", 200),
("salt", 10),
("clothes", 600), ] saving = input("please input your money:") #存入的本金
shopping_car = [] if saving.isdigit(): #判断存入的是不是数字
saving = int(saving)
while True:
for i,v in enumerate(product_list,1):
print(i,">>>>",v)
choice = input("选择购买商品编号[退出:q]:") if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
p_item = product_list[choice - 1]
if p_item[1] < saving:
saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
shopping_car.append(p_item) #购物的信息
else:
print("余额不足,还剩%s,不能购买下面的商品"%saving)
print(p_item)
else:
print("您输入的编号不存在")
elif choice == "q":
print("--------您购买的商品如下--------")
for i in shopping_car:
print(i)
print("您还剩%s元钱"%saving)
break
else:
print("invalid input")
#购物车程序 product_list = [
("airplane",90000),
("pen", 80),
("Trek bike", 5000),
("Book", 200),
("salt", 10),
("clothes", 600), ] saving = input("please input your money:") #存入的本金
shopping_car = [] if saving.isdigit(): #判断存入的是不是数字
saving = int(saving)
while True:
for i,v in enumerate(product_list,1):
print(i,">>>>",v)
choice = input("选择购买商品编号[退出:q]:") if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
p_item = product_list[choice - 1]
if p_item[1] < saving:
saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
shopping_car.append(p_item) #购物的信息
else:
print("余额不足,还剩%s,不能购买下面的商品"%saving)
print(p_item)
else:
print("您输入的编号不存在")
elif choice == "q":
print("--------您购买的商品如下--------")
for i in shopping_car:
print(i)
print("您还剩%s元钱"%saving)
break
else:
print("invalid input")
python_day4_shopping的更多相关文章
随机推荐
- js 时间特效
http://example.com:1234/test.htm#part2:Hash的作用. http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
- 服务器ipmi远程管理
DELL iDRAC (Integrated Dell™ Remote Access Controller )是 Dell PowerEdge 系列服务器上的远程管理方案, 11代 12代服务器已经集 ...
- liunx增强命令
查找命令 grep 格式:grep [option] pattern [file] 实例: ps -ef | grep sshd 查找指定 ssh 服务进程 ps -ef | grep sshd | ...
- spring对数据库的操作、spring中事务管理的介绍与操作
jdbcTemplate的入门 创建maven工程 此处省略 导入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/s ...
- 1.6 WEB API NET CORE 使用Redis
一.Redis安装 https://www.cnblogs.com/cvol/p/9174078.html 二.新建.net core web api程序 选webapi 或者应用程序都可以 三.使用 ...
- jquery表单
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 生产环境rac无法启动
节点二crs无法启动,查看启动日志:ohasd.log位置在/u01/app/11.2.0/grid/log/host01/ohasd/ohasd.log另外root.sh的log在rootcrs_X ...
- 《3D打印:从想象到现实》:基本没发现独到之处
本书汇总了3D打印的相关咨询:原理.可能的或已经实现的应用.商业模式等等.由于3D打印是最近媒体上比较热的信息,对我来说书中的大部分内容都没有独到之处,都是已知的.
- 【转】Android应用程序窗口(Activity)窗口对象(Window)创建指南
在前文中,我们分析了Android应用程序窗口的运行上下文环境的创建过程.由此可知,每一个Activity组件都有一个关联的ContextImpl对象,同时,它还关联有一个Window对象,用来描述一 ...
- 根据ip确定城市
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...