# 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. jquery mobile demo

    <!DOCTYPE html> <html> <head> <title>jQuery Mobile Demo</title> <me ...

  2. 第二百三十三节,Bootstrap表格和按钮

    Bootstrap表格和按钮 学习要点: 1.表格 2.按钮 本节课我们主要学习一下 Bootstrap 表格和按钮功能,通过内置的 CSS 定义,显示各 种丰富的效果. 一.表格 Bootstrap ...

  3. 【BZOJ】3403: [Usaco2009 Open]Cow Line 直线上的牛(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3404 裸的双端队列.. #include <cstdio> #include <c ...

  4. chrome vim设置

    chrome vi 插件:Vrome 配置文件如下 set hintkeys=asdfghjkl;" 把默认使用数字来完成提示改为使用键盘上的这几个键map j 10jmap k 10k&q ...

  5. HDU 4970(杭电多校#9 1011题)Killing Monsters(瞎搞)

    题目地址:HDU 4970 先进行预处理.在每一个炮塔的火力范围边界标记一个点. 然后对每一个点的伤害值扫一遍就能算出来. 然后在算出每一个点到终点的总伤害值,并保存下来,也是扫一遍就可以. 最后在询 ...

  6. Ad Hoc Distributed Queries组件

    http://www.cnblogs.com/870060760JR/p/6016080.html SQL Server 阻止了对组件“Ad Hoc Distributed Queries”的 STA ...

  7. Oracle菜鸟之怎样创建和删除表空间

    1.创建暂时表空间 创建表空间时,dbf文件一般存放在datafile文件的存放文件夹下(通常是在实例名命名的文件夹以下) 能够通过 select * from v$datafile;查看全部数据文件 ...

  8. Android无线测试之—UiAutomator UiScrollable API介绍三

    获取列表子元素 一.相关API介绍 返回值 API 描述 UiObject getChildByDescription(UiSelector childPattern, String text, bo ...

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

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

  10. 【NOI2015】品酒大会[后缀数组]

    #131. [NOI2015]品酒大会 统计 描述 提交 自定义测试 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项, ...