#author:leon
product_list= [
('iphone',5800),
('mac pro',9800),
('bike',800),
('watch',6000),
('coffee',31),
('book',120)
]
shopping_list= []
salary = input("input your salary:")
if salary.isdigit():
salary=int(salary)
while True:
for item in product_list:
print(product_list.index(item),item) #循环打印产品编号和产品列表(产品名加产品价格)
user_choice = input("选择买什么>>>>:")
if user_choice.isdigit(): #如果是数字成立,
user_choice=int(user_choice) #把数字转换成整型int
if user_choice<len(product_list) and user_choice>=0: #输入的数字长度必须小于产品列表的长度,#len()取列表长度
P_item=product_list[user_choice] #找出数字user_choice代表的值。例:name[1]找出产品
if P_item[1] <= salary: #买得起。列表product_list中嵌套了P_item元组。所以P_item[1],表示取出元祖中第二个值,即价格。
shopping_list.append(P_item)
salary -=P_item[1]
print("add %s into your shopping cart ,you current balance is \033[31;1m%s \033[0m"%(P_item,salary))
else:
print("\033[32;1m 你的余额不足 %s"%salary) elif user_choice =="q":
print(".....shopping list ...")
for p in shopping_list: #循环打印列表
print(p)
print("your balance %s " %salary)
exit()

cart_购物车小程序的更多相关文章

  1. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  2. [作业] Python入门基础---购物车小程序

    1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...

  3. python3 购物车小程序,余额写入文件保存

    python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...

  4. Day2:购物车小程序

    一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...

  5. python编写购物车小程序

     #练习#程序购物车#启动程序后,让用户输入工资,  然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额  ...

  6. Python之路 day2 购物车小程序1

    #Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...

  7. python写购物车小程序

    #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...

  8. python 练习购物车小程序

    # -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...

  9. 购物车小程序(while循环,列表)

    while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...

随机推荐

  1. javascript对下拉列表框(select)的操作

    <form id="f"> <select size="1" name="s"> <option value= ...

  2. Tomcat nginx log日志按天分割切割

    利用 Linux 自带的 logrotate 工具来实现按天切割日志.下方已 centos 7 系统为例来实践讲解. 原理 Logrotate是基于CRON来运行的,其脚本是/etc/cron.dai ...

  3. 03 Java图形化界面设计——布局管理器之FlowLayout(流式布局)

    前文讲解了JFrame.JPanel,其中已经涉及到了空布局的使用.Java 虽然可以以像素为单位对组件进行精确的定位,但是其在不同的系统中将会有一定的显示差异,使得显示效果不尽相同,为此java提供 ...

  4. 如何获得<div id=”div1”>This is first layer</div>中的值?

    如何获得<div id=”div1”>This is first layer</div>中的值? 解答: <script> var div1=Document.ge ...

  5. 网页端的utf8和gb2312 之间关于osd 传参数的乱码问题

    (0)HZK16 点阵字库原理及实现 (1)utf8 和 unicode gb2312之间的转换 (2)gb2312 的拓展 gbk 实现了更多的文字编码 像“瞭望塔”的瞭子在gb2312中是没有的 ...

  6. 蓝桥杯 C/C++参考题目 开平方(数学题,迭代法求开方)

    开平方 如果没有计算器,我们如何求2的平方根?可以先猜测一个数,比如1.5,然后用2除以这个数字.如果我们猜对了,则除法的结果必然与我们猜测的数字相同.我们猜测的越准确,除法的结果与猜测的数字就越接近 ...

  7. windows下用Eclipse连接大数据环境得hbase

    1.解压hbase安装包 2.将大数据环境得hadoop安装包拷贝到windows(这里以d:/hadoop为例) 3.打开C:\Windows\System32\drivers\etc目录下的hos ...

  8. JavaScript------如何解决表单登录信息输入为空显示提示

    <form name="fname" method="post" action="../Home/Login" onsubmit=&q ...

  9. 无法打开输入文件“optimized.lib” 编译osgEarth2.8+VS2013+CMake3.4.0在Release版本的问题

    1>LINK : fatal error LNK1181: 无法打开输入文件“optimized.lib” 可以到http://forum.osgearth.org搜索相关帖子,gwaldron ...

  10. JavaWeb开发的一系列可能的配置

    最近电脑硬盘坏了Orz...,重装了.唉!软件什么的都要重新装,重新设置,好麻烦! 我决定写下重装javaweb开发环境的过程 1.Java的安装,配置 https://www.runoob.com/ ...