[terry笔记]python购物程序
2024-09-01 04:21:43
如下是一个购物程序:
先输入工资,显示商品列表,购买,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
最新文章
- vim 学习积累(一)
- UIScrollView 性能优化 - view转为Image
- 禁止Visual Studio中的编译警告
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
- 学习php前需要了解的知识
- JS初学之-if else图片顺序及循环切换
- VBA在WORD中给表格外的字体设置为标题
- CP=";CAO PSA OUR"; 用P3P header解决iframe跨域访问cookie
- SOCKET的一些注意事项
- 想从事IT行业的你,一定看看这篇文章
- Hibernate之深入持久化对象
- 01_学习java WEB涉及到的相关技术
- 第四十四篇--做一个简单的QQ登录界面
- 2018-No.7-SicnuCtf
- golang 常量
- 安装Blend+SketchFlow Preview for Visual Studio 2012出现错误
- Windows下的Hadoop安装(本地模式)
- Gifts by the List CodeForces - 681D (思维)
- Windows远程桌面连接的利器-mRemote
- C语言学习记录_2019.02.09
热门文章
- POJ 2132
- 【WaaCaa】一款开源科学作图/数据可视化工具 —— 诞生篇
- Ant批量处理jmeter脚本
- PHP别名引用错误:“The use statement with non-compound name … has no effect”
- DNS递归查询和迭代查询的差别
- P1233 木棍加工
- DB-SQL-MySQL-杂项-调优:Mysql千万以上数据优化、SQL优化方法
- 给centos重新安装yum的base-repo源
- 深度理解Jquery 中 scrollTop() 方法
- Redis学习笔记(一) 初识 Redis