# 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 课堂笔记-购物车的更多相关文章

  1. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

  2. python 课堂笔记-while

    #Author:zyl age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:& ...

  3. python 课堂笔记-for语句

    for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...

  4. python 课堂笔记-if语句

    # Author:zyl _username = 'zyl' _password = 'zyl123' username = input("username:") password ...

  5. Python学习笔记六

    Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...

  6. Python学习笔记(十三)

    Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...

  7. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  8. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  9. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

随机推荐

  1. python笔记9 : 多线程

    基础: 什么是进程(process)? 每一个程序的内存是独立的,例如:world不能访问QQ. 进程:QQ是以一个整体的形式暴露给操作系统管理,里面包含了各种资源的调用(内存管理.网络接口调用等). ...

  2. 邮件正文及其附件的发送的C++实现

     这段代码我花了整整一天来编写,假设转载,请注明出处,谢谢!    前面的一篇文章已经讲了怎样发送邮件正文,原理我就不再叙述了.要了解的同学请到这里查看!    http://blog.csdn.ne ...

  3. 【Raspberry Pi】webpy+mysql+GPIO 实现手机控制

    1.mysql http://dev.mysql.com/doc/refman/5.5/en/index.html 安装 sudo apt-get install update sudo apt-ge ...

  4. 面试题思考:IOC的优缺点

    先讲重点  面试时怎么答: 先把IOC的概念说出来 依赖注入和控制反转 所谓的依赖注入是甲方开放接口,在它需要的时候,能够将乙方传递进来(注入):所谓的控制反转,甲乙双方不相互依赖,交易活动的进行不依 ...

  5. C++ 分割字符串两种方法

    原文地址:http://blog.csdn.net/xjw532881071/article/details/49154911 字符串切割的使用频率还是挺高的,string本身没有提供切割的方法,但可 ...

  6. HTML-CSS文件链接HTML的三种方式

    <!--css文本的链接方式有三种:分别是内联定义.链入内部css.和链入外部css--> <!--1.代码为:--> <!--<html> <head ...

  7. Python 基础之列表去重的几种玩法

    列表去重 1.方法1 借助一个临时列表 ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ids: if id not in news_ids ...

  8. [POJ] Brackets Sequence

    This problem can be solved elegantly using dynamic programming. We maintain two arrays: cnt[i][j] -- ...

  9. Pycharm创建Django admin用户名和密码

    1.Tools>Run manage.py Task 2.依次输入: makemigrations migrate createsuperuser 如: manage.py@production ...

  10. 巨蟒python全栈开发-第9天 初识函数

    一.今日主要内容总览(重点) 1.什么是函数? f(x)=x+1 y=x+1 函数是对功能或者动作的封装2.函数的语法和定义 def 函数名(): 函数体 调用:函数名()3.关于函数的返回值 ret ...