# 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. TaskTracker学习笔记

    转自:http://blog.csdn.net/androidlushangderen/article/details/41477061 上次分析完JobTracker通过TaskScheduler如 ...

  2. XMLHttpRequest的withCredentials属性

    最近对接第三方网站出现一下错误:Access to XMLHttpRequest at 'https://third.site.com/request_url' from origin 'https: ...

  3. MongoDB中关于查询条件中包括集合中字段的查询

    要查询的数据结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvMzY5NTE=/font/5a6L5L2T/fontsize/400/f ...

  4. AWS系列-修改RDS时间(time_zone)

    1.1 需求 数据库时间和北京时间相差8小时 开发人员发现app的时间与国内时间相差12小时,需要修改RDS的time_zone参数 1.2 打开RDS参数组 1.3 创建新的参数组 由于默认的参数组 ...

  5. JAVA-各种类型之间转换 parse() 与valueOf()

    类型互转 1.各种类型转String 2.String转Bytes 3.数组转List 4.进制转换 5. parse 在 SimpleDateFormat 中是转换为Date类,其它的一些包装类都是 ...

  6. knowledgeroot 的配置与优化

    首先下载 KnowledgeRoot 的安装包,就是一个压缩文件,解压缩后放到 WebRoot 下面 在浏览器中打开网站,自动提示进行安装,安装的过程很简单,安装结束后即可以使用. 安装包创建的数据库 ...

  7. 61、常规控件(4)TabLayout-便捷实现标签

    <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width=&q ...

  8. Leetcode-Convert Sorted List to BST.

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  9. 模态窗口原理及注意事项--http://www.alisdn.com/wordpress/?p=53

    前言 在开发Windows引用程序的时候,在一些需要用户确认,或者提示用户注意的场合,经常使用模态对话框,或者叫模态窗口.在绝大多数情况下,模态窗口给开发人员带来了极大的便利,并且在某些应用上有不可替 ...

  10. 机械迷城MAC下载及攻略

    点击下载 无意间在verycd上看到这个游戏,很好玩的一个游戏. 画风非常可爱,有点复古风. 这里是 机械迷城 的专题频道 http://pc.pcgames.com.cn/pczq/jxmc/