python 课堂笔记-购物车
# Author:leon
production_list = [
('iphone',5800), ('mac pro', 9800), ('bike', 800), ('watch', 10600), ('coffee', 31), ('alex python', 120)
]
shopping_list=[] #创建一个空列表,用于存放买到的商品。
salary = input("input your salary:") #输入工资
if salary.isdigit(): #判定输入的工资是否是数字
salary = int(salary) #输入的工资是数字成立,把工资强制转换为整型。
while True:
for item in production_list: #第一种取下标方法
print(production_list.index(item),item)
#for index,item in enumerate(production_list): #第二种取下标方法
# print(index,item)
user_choice = input("选择要买的商品>>>:")
if user_choice.isdigit(): #判断数据是否是数字
user_choice= int(user_choice) #如果是数据,就把数据转为整型。
if user_choice < len(production_list) and user_choice >=0: #获取列表长度,返回数字,即为列表长度
p_item=production_list[user_choice] #获取商品,并赋值给p_item
if p_item[1] <= salary: #获取商品的价格,并判断商品的价格是否小于等于工资
shopping_list.append(p_item) #条件成立,把买到的商品放到空列表中
salary -=p_item[1] #把工资数减去所购买的商品价格,结果赋值给salary,此时结果为所剩余额
print("Added %s into shopping cart, you current balance is %s" %(p_item,salary))
else:
print("\033[31;1m 您的余额不足,只剩%s.\033[0m" %salary)
else:
print("\033[31;1m 商品不存在 \033[0m")
elif user_choice == 'q':
print("-----------shopping list ----------")
for p in shopping_list:
print(p)
print("your current balance:",salary)
exit()
else:
print("invalid option")
python 课堂笔记-购物车的更多相关文章
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- python 课堂笔记-while
#Author:zyl age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:& ...
- python 课堂笔记-for语句
for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...
- python 课堂笔记-if语句
# Author:zyl _username = 'zyl' _password = 'zyl123' username = input("username:") password ...
- Python学习笔记六
Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...
- Python学习笔记(十三)
Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...
- python学习笔记目录
人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...
- Web Scraping with Python读书笔记及思考
Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
随机推荐
- JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language
JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language,结构化查询语言)数据库访问接口,它使数据 ...
- 在mybatis 中批量加载mapper.xml
可以直接加载一个包文件名,将这个包里的所有*mapper.xml文件加载进来. 指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载: 必须按一定的标准:即xml文件和 ...
- The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha ...
- hdu 5360 Hiking(优先队列+贪心)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...
- 转(解决GLIBC_2.x找不到的编译问题)
Linux/CentOS 升级C基本运行库CLIBC的注意事项(当想解决GLIBC_2.x找不到的编译问题) 分类: 开发环境 Linux2014-09-24 10:32 8933人阅读 评论(5) ...
- 基础-Eclipse 教程
1.Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台.2.下载地址为: https://www.eclipse.org/downloads/.3.Eclipse 修改字符集 : W ...
- 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树
[BZOJ3674]可持久化并查集加强版 Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了! ...
- 【BZOJ3261】最大异或和 Trie树+贪心
[BZOJ3261]最大异或和 Description 给定一个非负整数序列 {a},初始长度为 N. 有 M个操作,有以下两种操作类型:1 .A x:添加操作,表示在序列末尾添加一个 ...
- storyboard设置navigation controller
到storyboard选中我们唯一一个的viewcontroller,找到xcode的菜单栏,Edit->Embed In->NavigationController.这时候storybo ...
- wordpress添加关键字
wordpress自动添加标签为关键字: <?php //判断是否为首页 if ( is_home ()) { $description = "jcomey一个文艺青年的个人博客&qu ...