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的更多相关文章
随机推荐
- greenplum维护
1.用户管理 psql -d sea CREATE DATABASE BI; CREATE USER ubi WITH PASSWORD 'pwdbi' NOSUPERUSER; GRANT ALL ...
- Mantis中的状态
在 Mantis中的 问题状态一共有以下几种 10:new,20:feedback,30:acknowledged,40:confirmed,50:assigned,80:resolved,90:cl ...
- 详解Javaweb中常见漏洞的防御
上一篇给大家介绍了SpringMVC中常见的客户端数据输入点,这一篇给大家讲解下java中常见漏洞的防御方法. 0x01.sql注入 下面我们就用利用SpringMVC自带的数据库操作类jdbcTem ...
- 一.配置简单的嵌入式tomcat和jetty
我们写了一个web应用,打成war包后,就需要找一个server来部署.对于我们的实际应用,我们基本没必要自己再写一个嵌入式的server.接下来两篇文章只是以钻研的心态来学习一下嵌入式tomcat和 ...
- IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)
最近项目要交付了,对方安全测试的时候检测出高危险漏洞,由于刚参加工作不久,经验不足,未涉及过此方面的东西.经过一番查询和探索,最终解决了这个问题,记录一下. 发现的漏洞为缺少跨框架脚本保护.跨框架脚本 ...
- [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...
- IOS 播放视频(MPMoviePlayerController、MPMoviePlayerViewController)
● iOS提供了叫 做MPMoviePlayerController.MPMoviePlayerViewController的两个 类,可以用来轻松播放视频 ➢ YouTobe就是用MPMoviePl ...
- 有趣的npx
在更新 npm 5.2.0 的时候发现会买一送一,自动安装了 npx. npx 会帮你执行依赖包里的二进制文件,也就是说 npx 会自动查找当前依赖包中的可执行文件, 如果找不到,就会去 PATH 里 ...
- 【luogu P2319 [HNOI2006]超级英雄】 题解
题目链接:https://www.luogu.org/problemnew/show/P2319 #include <cstdio> #include <cstring> #i ...
- Android学习笔记_71_Android 多个项目之间如何引用 项目怎样打jar包
一.将整个项目作为资源文件 1.需要将被应用的项目设置为库项目. 2.将该项目的配置文件中的四大组件清空,例如下面代码: <?xml version="1.0" encodi ...