学python第三天购物车程序:

#/bin/env python3
# _*_ utf-8 _*_List='''1. bike 800
2. cat 90000
3. book 100
'''
List_list=(("1","bike",800),("2","cat","90000"),("3","book","100"))
count = 0
sary=int(input("please input your sary:"))while True:print(List)
#    Input = int(input("please input(按q退出):"))Input = input("please input(按q退出):")
#    print(isinstance(Input,int))if Input == "1":count = count + 800elif Input == "2":count = count + 90000elif Input == "3":count = count + 100elif Input == "q":break;print("you buy things is",count)
if sary>=count:Yes=input("are you sure to buy?")while Yes == "y":sary=sary-countprint("now,your sary is",sary)break
else:print("your sary is too small")

老师的写法:

#/bin/env python3
# _*_ utf-8 _*_
product_list=[('Mac',9000),('kindle',800),('tesla',900000),('python book',105),('bike',2000),]
saving=input('please input your money:')
shopping_car=[]
if saving.isdigit():saving=int(saving)while True:#打印商品内容for i,v in enumerate(product_list,1):           #enumerate 会把列表加上序号,默认从0开始;这里指定了从数字1开始print(i,'>>>>',v)#引导用户选择商品choice=input('选择购买商品编号[退出:q]:')#验证输入是否合法if choice.isdigit():choice=int(choice)if choice>0 and choice<=len(product_list):#将用户选择商品通过choice取出来p_item=product_list[choice-1]#如果钱够,用本金saving减去该商品价格,并将该商品加入购物车if p_item[1]

学了文件操作之后,购物车程序改进版:

#/bin/env python3
# _*_ utf-8 _*_
# __author__=wjz
# 需要在当前目录下建立sary.txt文件,内容存储为字典格式,用户名和金额。例如 {'wjz':1000,'wjk':500}import timeshop_dict={'咖啡':22,'牛奶':5,'百岁山':4,'果珍':9,'甜点':15,'冰茶':11,'红茶':12}shop_list=list(shop_dict)
shop_list.sort()
shop_tuple=tuple(shop_list)f=open('sary.txt',"r",encoding="utf8")
sary_json=eval(f.read().strip())
f.close()
username=input("please input your name:")
sary=sary_json[username]
print("your sary is %d now." %sary)num=0
for i in shop_tuple:if num==0:print("shop is this:")num+=1print('---> ('+str(num)+')',i,shop_dict[i])shop_car=[]
while True:user_input=input("please input your select(q for exit):".strip())if user_input == 'q':breakelif not user_input.isnumeric():print("please input num")continueelif int(user_input) > len(shop_tuple) or int(user_input) <= 0 :print("please input right num")continueelse:user_input=int(user_input)select_shop=shop_tuple[user_input-1]if sary >= shop_dict[select_shop]:sary=sary - shop_dict[select_shop]shop_car.append(select_shop)print("%s has been in shopping car. sary is %d now." % (select_shop,sary))else:print("your money is not enough.")time.sleep(0.1)tuple_shop_car=tuple(shop_car)
set_shop_car=set(tuple_shop_car)
buyd=""
for i in set_shop_car:currut_row="%s: %d" %(i,tuple_shop_car.count(i))buyd=buyd+"\n"+currut_row
print('''your shop car is:\n----------%s\n----------''' %buyd)while True:sure_pay=input("Are you sure to pay?[y/n]")if sure_pay == "y":sary_json[username]=saryf=open('sary.txt','w')f.write(str(sary_json))f.close()print("your sary is %d now." %sary)breakelif sure_pay == "n":breakelse:print("please input your selection.")

转载于:https://www.cnblogs.com/hacker001/p/10081852.html

金角大王——购物车程序相关推荐

  1. python购物车程序2019_Python实现购物车程序

    本文实例为大家分享了程序:Python购物车程序,具体内容如下 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就 ...

  2. 基于cookies的小型购物车程序

    基于cookies的小型购物车程序 最近因为公司项目开发,需要一个购物车程序. 虽然网上很多,但个人很久前就想写个.于是马上动手. 代码有点多,感觉方法不怎么好! 随便post上来,高手可略过~! m ...

  3. python 购物车分析_python 简易购物车程序解析

    购物车程序需求:用户信息为: {'wyf':{'password': '12', 'role': '2', 'money': '100000', 'carts': ['iphone6', 'mi5'] ...

  4. python购物车程序2019_Python——购物车程序(列表的应用)

    购物车基本功能: 1.展示购物清单.价格信息. 2.由客户选择商品序号.加入购物车. 3.结束购物后,展示购物车中的商品信息,余额信息. 其他规则:所选商品价格超出当前余额时,提示钱不够: 直到客户输 ...

  5. 运用循环判断语句和列表的购物车程序

    针对循环判读语句和列表的运用练习,对应Day2中的第一个购物车程序训练. 能力有限,可能存在不足. 1 # Author: JC 2 3 while 1: 4 balance = input(&quo ...

  6. Python自学----- while if 列表结合,购物车程序

    # -*- coding: UTF-8 -*- #!/usr/bin/env python#购物车程序salary = int(input('请输入的工资:')) #输入工资! y ='y' #定义退 ...

  7. python 购物车程序_Python 购物车程序(文件版)

    ''' 购物车程序 用户入口: 1.商品信息存在文件里 2.已购商品,余额记录 商家入口: 1.可以添加商品,修改商品价格 ''' filePath = "D:\Python_work\Lp ...

  8. python 购物车程序_购物车程序python

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 第3点要求: 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒在循环外层 ...

  9. Day2 - Python基础2作业【购物车程序】

    1 #/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 # Day2/shopping_cart.py 4 5 _author_ = 'hepidong' 6 ...

最新文章

  1. Linux下配置SNMP
  2. redis开启远程连接
  3. 实现弹出悬浮页面_30秒快速实现Excel每页标题行的打印!
  4. Python从入门到项目实践(明日科技 吉林大学出版社)
  5. 50:树中两个结点的最低公共祖先
  6. Oracle Cluster Health Monitor(CHM)简介
  7. php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法
  8. Nginx 屏蔽ip地址的方法
  9. mysql dba命令_MySQL DBA命令
  10. OpenCV C++ 常用功能
  11. duilib显示html,Duilib 控件类html富文本绘制
  12. 前端实战:从零到一实现H5拼图小游戏(附源码)
  13. 联想 G50-70 关闭无线功能,如何重新启动??
  14. 代码审查工具 phabricator 使用学习
  15. andriod自带模拟器使用代理
  16. 九爷带你了解 Tomcat 优化
  17. 第七章 突变和随机化
  18. 计算机专业名词(缩写、全称、中文全称)
  19. python笔记 之 手机号有效性简单判断
  20. 个人总结的一个VMP脱壳步骤.

热门文章

  1. java内联函数_Java之内联函数_内联函数的优缺点
  2. EEGLAB数据处理学习
  3. 如何在注释里优雅地吐槽
  4. 深度学习--word2vec和doc2vec词向量模型
  5. 目标跟踪项目 问题及解决方法记录
  6. OSChina 周四乱弹 —— 激光剑常年开着不费电嘛
  7. 【spring源码】基于factoryMethod创建bean对象
  8. signature=4f0a1c9773fc07031e260b016c514c92,针对协同过滤推荐系统的推举托攻击研究
  9. 170+道钉钉前端扫盲知识点助你掌握前端基础
  10. 算法复杂度与性能的解释与计算方法