如下是一个购物程序:

先输入工资,显示商品列表,购买,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购物程序的更多相关文章

  1. [terry笔记]python FTP

    如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...

  2. [terry笔记]Python字符串

    如下学习python的字符串用法. print(dir(str)) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', ...

  3. [terry笔记]python内置函数

    总结一下内置函数,Build-in Function. 一.数学运算类 abs(x) 求绝对值 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取商和余数注 ...

  4. [terry笔记]python三级菜单

    把三级菜单输出,选择后逐层显示,”b“返回上一级菜单. menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村': ...

  5. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  6. python 整型--《Python 3程序开发指南》笔记

    参考:<Python 3程序开发指南> 整数转换函数: bin(i) 返回整数i的二进制表示(字符串) hex(i) 返回i的十六进制表示(字符串) int(x) 将x转换为整数,失败产生 ...

  7. MongoDB学习笔记:Python 操作MongoDB

    MongoDB学习笔记:Python 操作MongoDB   Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...

  8. 运行easy_install安装python相关程序时提示failed to create process

    运行easy_install安装python相关程序时提示failed to create process,因为安装了两个python,卸载了的那个目录没删除,删除了另外的python目录后这个问题就 ...

  9. Apache运行python cgi程序

    Apache运行python cgi程序 环境 win10 x64 专业版 Apache2.4 python 2.7 Apache安装和配置 Apache服务器的安装请自行搜索.在Apache2.4中 ...

随机推荐

  1. Java Web学习总结(27)——JavaEE中Web服务器、Web容器、Application服务器区别及联系

    在JavaEE 开发Web中,我们经常会听到Web服务器(Web Server).Web容器(Web Container).应用服务器(Application Server),等容易混淆不好理解名词. ...

  2. [SharePoint2010开发入门经典]四、开发者常见任务

    本章概要: 1.创建不同种类的web部件,包括标准的,可视化的还有数据绑定web部件 2.理解列和内容类型,如何使用它们创建列表 3.理解如何与SPS交互,使用SPS API调用数据 4.创建编辑页面 ...

  3. SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)

    SQL语法精讲(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELE ...

  4. kaggle 中使用ipython

    # pandas import pandas as pd from pandas import Series,DataFrame # numpy, matplotlib, seaborn import ...

  5. oracle性能检测sql语句

    1. 监控事例的等待 select event,sum(decode(wait_Time,0,0,1)) "Prev",sum(decode(wait_Time,0,1,0)) & ...

  6. layer.js漂亮的弹出框

    它的官方网站:http://layer.layui.com/ 消息.确认框.ifame.自定义文本.旋转木马,都有按钮,是一款强大的js 弹出框: 以下为本人的简单介绍: layer.open({ t ...

  7. struts2学习之基础笔记3

    第8章Struts 2类型转换 使用类型转换器 自定义类型转换器 步骤:1. Struts 2 构建流程 2.自定义类型转换器类(继承 DefaultTypeConverter /StrutsType ...

  8. js 数据类型判断

    判断type类型 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } ...

  9. (转载)解决切换Fragment时布局重新实例化

    解决切换Fragment时布局重新实例化 作者 instanceof 关注 2015.12.30 17:25* 字数 628 阅读 7616评论 17喜欢 23 关于Fragment 在这简单说一下F ...

  10. RecyclerView的刷新和加载更多

    1.RecyclerView :出现也不知道多久了,没怎么使用过,上次写的笔记乱七八糟的,再次仔细的整理下.   使用需加入依赖  compile 'com.android.support:recyc ...