购物车例子,实现显示商品信息,输入商品编号并且可以减去自己的存入余额,当商品价格大于自己的余额的时候,直接退出;当不再选择商品的时候,退出显示余额和已经添加的商品。

#购物车程序

product_list = [
("airplane",90000),
("pen", 80),
("Trek bike", 5000),
("Book", 200),
("salt", 10),
("clothes", 600), ] saving = input("please input your money:") #存入的本金
shopping_car = [] if saving.isdigit(): #判断存入的是不是数字
saving = int(saving)
while True:
for i,v in enumerate(product_list,1):
print(i,">>>>",v)
choice = input("选择购买商品编号[退出:q]:") if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
p_item = product_list[choice - 1]
if p_item[1] < saving:
saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
shopping_car.append(p_item) #购物的信息
else:
print("余额不足,还剩%s,不能购买下面的商品"%saving)
print(p_item)
else:
print("您输入的编号不存在")
elif choice == "q":
print("--------您购买的商品如下--------")
for i in shopping_car:
print(i)
print("您还剩%s元钱"%saving)
break
else:
print("invalid input")
#购物车程序

product_list = [
("airplane",90000),
("pen", 80),
("Trek bike", 5000),
("Book", 200),
("salt", 10),
("clothes", 600), ] saving = input("please input your money:") #存入的本金
shopping_car = [] if saving.isdigit(): #判断存入的是不是数字
saving = int(saving)
while True:
for i,v in enumerate(product_list,1):
print(i,">>>>",v)
choice = input("选择购买商品编号[退出:q]:") if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
p_item = product_list[choice - 1]
if p_item[1] < saving:
saving -= p_item[1] #减去添加购物车的钱,还剩下的钱数
shopping_car.append(p_item) #购物的信息
else:
print("余额不足,还剩%s,不能购买下面的商品"%saving)
print(p_item)
else:
print("您输入的编号不存在")
elif choice == "q":
print("--------您购买的商品如下--------")
for i in shopping_car:
print(i)
print("您还剩%s元钱"%saving)
break
else:
print("invalid input")

python_day4_shopping的更多相关文章

随机推荐

  1. mysql的sql_mode介绍和修改

    原文链接: http://blog.csdn.net/wulantian/article/details/8905573   mysql目录下有一个配置文件my.conf. mysql数据库有一个环境 ...

  2. Android学习——ViewPager的使用(二)

    这一节介绍使用FragmentPagerAdapter适配器,来加载Fragment对象. 数据源 加载Fragment对象时,数据源自然来自Fragment,与View类似,依旧使用List来存放数 ...

  3. Linux中基于apache httpd的svn服务器搭建与配置

    mod_dav_svn是apache连接svn的模块 yum install subversion mod_dav_svn httpd 配置文件简单说明, SVNParentPath 说明可以在指定的 ...

  4. Linux中如何安装配置Mysql和SVN服务端

    目标Linux系统为centOS 一.安装登陆mysql   1.直接以root用户运行:yum install mysql 和yum install mysql-server等带安装完成. 2.安装 ...

  5. startup ORA-00845: MEMORY_TARGET not supported on this system

    一台虚拟机跑多个实例时,由于/dev/shm空间不够导致如下报错> startupORA-00845: MEMORY_TARGET not supported on this system解决方 ...

  6. JS入口函数和JQuery入口函数

    首先,讲一下它们的区别: (1)JS的window.onload事件必须要等到所有内容,以及外部图片之类的文件加载完之后,才会去执行. (2)JQuery入口函数是在所有标签加载完之后,就会去执行. ...

  7. Orchard core 中文文档翻译系列

    本系列翻译顺序完全参照 官方顺序 原文地址:https://orchardcore.readthedocs.io/en/latest/ Orchard Core 中文文档翻译(一)关于Orchard ...

  8. April 1 2017 Week 13 Saturday

    There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. Get a life, a real life, not a manic pu ...

  9. 简单的PHP算法题

    简单的PHP算法题 目录 1.只根据n值打印n个0 2.根据n值打印一行 0101010101010101010101…… 3.根据n值实现1 00 111 0000 11111…… 4.根据n值实现 ...

  10. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...