cart_购物车小程序
#author:leon
product_list= [
('iphone',5800),
('mac pro',9800),
('bike',800),
('watch',6000),
('coffee',31),
('book',120)
]
shopping_list= []
salary = input("input your salary:")
if salary.isdigit():
salary=int(salary)
while True:
for item in product_list:
print(product_list.index(item),item) #循环打印产品编号和产品列表(产品名加产品价格)
user_choice = input("选择买什么>>>>:")
if user_choice.isdigit(): #如果是数字成立,
user_choice=int(user_choice) #把数字转换成整型int
if user_choice<len(product_list) and user_choice>=0: #输入的数字长度必须小于产品列表的长度,#len()取列表长度
P_item=product_list[user_choice] #找出数字user_choice代表的值。例:name[1]找出产品
if P_item[1] <= salary: #买得起。列表product_list中嵌套了P_item元组。所以P_item[1],表示取出元祖中第二个值,即价格。
shopping_list.append(P_item)
salary -=P_item[1]
print("add %s into your shopping cart ,you current balance is \033[31;1m%s \033[0m"%(P_item,salary))
else:
print("\033[32;1m 你的余额不足 %s"%salary) elif user_choice =="q":
print(".....shopping list ...")
for p in shopping_list: #循环打印列表
print(p)
print("your balance %s " %salary)
exit()
cart_购物车小程序的更多相关文章
- python 购物车小程序
python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...
- [作业] Python入门基础---购物车小程序
1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...
- python3 购物车小程序,余额写入文件保存
python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...
- Day2:购物车小程序
一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...
- python编写购物车小程序
#练习#程序购物车#启动程序后,让用户输入工资, 然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额 ...
- Python之路 day2 购物车小程序1
#Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...
- python写购物车小程序
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...
- python 练习购物车小程序
# -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...
- 购物车小程序(while循环,列表)
while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...
随机推荐
- is_array
is_array (PHP 4, PHP 5) is_array — 检测变量是否是数组
- Adversarial Training
原于2018年1月在实验室组会上做的分享,今天分享给大家,希望对大家科研有所帮助. 今天给大家分享一下对抗训练(Adversarial Training,AT). 为何要选择这个主题呢? 我们从上图的 ...
- Oracle启动中,spfile.ora、init<SID>.ora、spfile<SID>.ora 这三个文件正确的先后顺序是什么?
Oracle启动中,spfile.ora.init<SID>.ora.spfile<SID>.ora 这三个文件正确的先后顺序是什么? 解答:启动数据库,使用startup命令 ...
- Java容器:HashMap和HashSet解析
转载请注明出处:jiq•钦's technical Blog 一.HashMap HashMap,基于散列(哈希表)存储"Key-Value"对象引用的数据结构. 存入的键必须具备 ...
- mysq for visual studio 1.1.1
https://cdn.mysql.com/Downloads/MySQLInstaller/mysql-visualstudio-plugin-1.1.1.msi
- (转)Unity笔记之编辑器(Foldout、HelpBox、InspectorTitlebar、Slider、MinMaxSlid ...
1. Foldout.HelpBox 折叠菜单,大家都知道,不具体解释了,直接代码.因为折叠菜单中必然是有内容才能看到效果,所以顺带把HelpBox(提示框)也说了. [code]csharpcode ...
- css各居中大法
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- 标签球-Js插件
今天上学校的图书馆,看到了一个好玩的东西,特意百度了下,发现叫做“标签球”,效果图为: 直接代码如下: #div1 {position:relative; width:350px; height:35 ...
- svn管理的项目迁移到tfs
1.将.vs .svn文件夹删除 2.连接tfs,签入代码
- PDO防止sql注入的机制
使用PDO訪问MySQL数据库时,真正的real prepared statements 默认情况下是不使用的. 为了解决问题,你必须禁用 prepared statements的仿真效果. 以下是使 ...