[terry笔记]python购物程序
如下是一个购物程序:
先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品。
count = 0
while True: #做一个循环判断,如果输入的不是数字,基于提示,三次后退出
salary = input("input your salary:") #输入你的工资
if salary.isdigit(): #输入的工资必须是数字才能往下走
salary=int(salary) #转换为整数型
break
else:
print("Please input a number:")
count += 1
if count == 3:
exit() goods_list = [["Iphone",5800],["Macbook",12800],["iMac",15000],["ApplePen",500],["IPod",1200]] #商品列表
shop_list = [] #购买的商品列表
msg = " Product List "
print(msg.center(30,"*"))
for i,ele in enumerate(goods_list): #遍历序列中的元素以及它们的下标
print(i,".",ele[0],ele[1])
while True:
choice = input("Which do you want(quit type \"quit\")>>>")
if choice.isdigit(): #判断选择的是否是数字
choice = int(choice) #转换为整数型
if choice <= len(goods_list)-1: #选择的数字必须小于列表长度-1,因为下标从0开始
if salary >= int(goods_list[choice][1]): #判断工资是否够
shop_list.append(goods_list[choice]) #够的话,把商品加入到shopList
salary -= goods_list[choice][1] #减去工资
print("You just buy a %s now you have %s RMB" % (goods_list[choice][0],salary))
else:
print("Not enough money")
else:
print("There is no such things")
elif choice == "quit":
print("Here is what you buy:") #这里的思路是,创建一个字典,把所买的商品格式化输出
total = 0
shop_dict={}
for item in shop_list:
things = item[0]
money = item[1]
total += int(money)
if things in shop_dict:
shop_dict[things][0] += 1
shop_dict[things][1] += money
else:
shop_dict[things]=[1,money]
for item in shop_dict.items():
print("%s %s个 共%s" % (item[0],item[1][0],item[1][1]))
print("一共花了:",total)
exit()
else:
print("Please input a number")
continue
[terry笔记]python购物程序的更多相关文章
- [terry笔记]python FTP
如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...
- [terry笔记]Python字符串
如下学习python的字符串用法. print(dir(str)) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', ...
- [terry笔记]python内置函数
总结一下内置函数,Build-in Function. 一.数学运算类 abs(x) 求绝对值 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取商和余数注 ...
- [terry笔记]python三级菜单
把三级菜单输出,选择后逐层显示,”b“返回上一级菜单. menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村': ...
- python学习笔记-python程序运行
小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...
- python 整型--《Python 3程序开发指南》笔记
参考:<Python 3程序开发指南> 整数转换函数: bin(i) 返回整数i的二进制表示(字符串) hex(i) 返回i的十六进制表示(字符串) int(x) 将x转换为整数,失败产生 ...
- MongoDB学习笔记:Python 操作MongoDB
MongoDB学习笔记:Python 操作MongoDB Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...
- 运行easy_install安装python相关程序时提示failed to create process
运行easy_install安装python相关程序时提示failed to create process,因为安装了两个python,卸载了的那个目录没删除,删除了另外的python目录后这个问题就 ...
- Apache运行python cgi程序
Apache运行python cgi程序 环境 win10 x64 专业版 Apache2.4 python 2.7 Apache安装和配置 Apache服务器的安装请自行搜索.在Apache2.4中 ...
随机推荐
- H3C防火墙——回环流量问题(内网终端通过外网IP访问内部服务器)
http://www.bubuko.com/infodetail-1533703.html
- tomcat设置编码utf8
1. Java类: CharacterEncodingFilter import javax.servlet.*; import java.io.IOException; public ...
- [ASP.NET]EF跨项目调用问题
在一个项目中调用另一个项目中的模型,在该项目中添加一个模型,解决自动提示问题using问题
- Spring+MyBatis双数据库配置
Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...
- XCODE插件 之 Code Pilot 无鼠标化
什么是Code Pilot? Code Pilot 是一个 Xcode 5 插件.同意你不许使用鼠标就能高速地查找项目内的文件.方法和标识符. 它使用模糊查询匹配(fuzzy query matchi ...
- 纪念2014 TI DSP大奖赛
偶然发现TI官网有新闻报道大奖赛,还有沈洁女士给我们颁奖的照片.纪念一下. 第六届TI DSP及嵌入式大奖赛决赛暨颁奖典礼在厦门大学成功举行 Frances Han 2013-2014 TI DSP ...
- poj 2528 Mayor's posters 【线段树 + 离散化】
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50643 Accepted: 14675 ...
- 0x17 二叉堆
优先队列太好用了手写啥呀 poj1456 经过贪心专题的洗礼以后这题根本就不叫题啊...按时间大到小排每次取最大就好 #include<cstdio> #include<iostre ...
- numpy的scale就是 x-mean/std
>>> from sklearn import preprocessing >>> import numpy as np >>> a=np.arr ...
- 转:utf8汉字编码16进制对照
http://blog.chinaunix.net/uid-25544300-id-3281847.html GB Unicode UTF-8 Chinese Character Co ...