cart_购物车小程序
#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_购物车小程序的更多相关文章
- python 购物车小程序
python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...
- [作业] Python入门基础---购物车小程序
1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...
- python3 购物车小程序,余额写入文件保存
python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...
- Day2:购物车小程序
一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...
- python编写购物车小程序
#练习#程序购物车#启动程序后,让用户输入工资, 然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额 ...
- Python之路 day2 购物车小程序1
#Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...
- python写购物车小程序
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...
- python 练习购物车小程序
# -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...
- 购物车小程序(while循环,列表)
while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...
随机推荐
- 在线生成条形码的解决方案(39码、EAN-13)
感谢博主:转自:http://xoyozo.eyuyao.com/blog/barcode.html public partial class ReceivablesFormView : System ...
- 补图BFS(hdu 5876)
题目大意: 给出一个图和起点S,求补图中S到其他点的最短距离. http://acm.hdu.edu.cn/showproblem.php?pid=5876 我自己的垃圾做法: 用线段树来维护dijk ...
- Git—怎样Windows操作系统中安装Git
介绍一下怎样在Windows操作系统中安装Git: 一.下载Git安装压缩文件:http://download.csdn.net/detail/wangshuxuncom/8035045 二.解压该压 ...
- 用python解析html--SGMLParser
sgmllib.py 包含一个重要的类: SGMLParser.SGMLParser 将 HTML 分解成有用的片段, 比如开始标记和结束标记.一旦它成功地分解出某个数据为一个有用的片段,它会根据 所 ...
- python3----生成器generator(yield)
# 列表推导式a = [i for i in range(100) if not(i % 2) and (i % 3)]print(a)# 字典推导式b = {i: i % 2 == 0 for i ...
- VMThread占CPU高基本上是JVM在频繁GC导致,原因基本上是冰法下短时间内创建了大量对象堆积造成频繁GC。
今天线上一个java进程cpu负载100%.按以下步骤查出原因. 1.执行top -c命令,找到cpu最高的进程的id 2.执行top -H -p pid,这个命令就能显示刚刚找到的进程的所有线程的资 ...
- linux串口编程参数配置详解
1.linux串口编程需要的头文件 #include <stdio.h> //标准输入输出定义 #include <stdlib.h> //标准函 ...
- 160718、jsoup-1.8.1.jar操作html
导入jsoup-1.8.1.jarimport java.io.IOException;import org.jsoup.Connection;import org.jsoup.Jsoup;impor ...
- powershell Start-Sleep
秒: Start-Sleep –s 10 ,毫秒) Start-Sleep –m 10000 语法 Start-Sleep [-seconds] <int> [<CommonPara ...
- Ansi、GB2312、GBK、Unicode(utf8、16、32)
关于ansi,一般默认为本地编码方式,中文应该是gb编码 他们之间的关系在这边文章里描写的很清楚:http://blog.csdn.net/ldanduo/article/details/820353 ...