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. 返 ...
随机推荐
- Windows下RabbitMQ安装,部署,配置
安装部署 1.当前环境以及参考资料出处 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com/install- ...
- Spring MVC学习-----------springMVC-mvc.xml
springMVC-mvc.xml 配置文件片段解说 (未使用默认配置文件名称) <?xml version="1.0" encoding="UTF-8" ...
- 【R】均值假设检验
p_value<-function(cdf,x,parament=numeric(0),side=0) { n<-length(parament) p<-switch(n+1, cd ...
- 《Start Developing iOS Apps Today》摘抄
原文:<Start Developing iOS Apps Today> Review the Source Code 入口函数main.m #import <UIKit/UIKit ...
- asscert断言的几种方法
一.什么是断言 执行完测试用例后,最后一步是判断测试结果是通过还是失败,在自动化脚本中一般把这种生成测试结果的方法叫做断言 它用来检查一个条件,如果它为真,则不做任何事,如果它为假,则会跑出Asser ...
- 修改MyEclipse内存
Window ---> preferrences-->installed JREs :点击JDK修改argument为:-Xms512m -Xmx1024m -XX:PermSize= ...
- Centos 虚拟机网络问题,网卡起不来,重启network服务失败
拷贝了个虚拟机,有两个网卡,1个可以起来,另一个起不来.运行命令:$>systemctl restart network 输出如下:Job for network.service failed ...
- 网站速度优化模块HttpCompressionModule
为了优化网站的访问速度,准备采用HttpCompressionModule 6对传输数据进行压缩,下载了HttpCompressionModule 6 , 并按照示例程序中的web.config配置了 ...
- c++ 类声明
class B; struct A { B* ptr; }; class B { public: }; int main() { ; } A中定义了B的指针,所以要声明class B,在定义处于不完整 ...
- hdu3625(第一类斯特林数)
与第二类有些区别! #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...