python自动化开发实战实战2:用列表与文件创建购物系统
需求:
一、商家界面
商家可根据自身需求添加和修改商品。并保存到文件中
二、顾客端
- 顾客登录系统,如果是初次登录,需输入金额;再次登录则不用,直接显示商品清单界面
- 可保存顾客购买商品信息与价格,下次登录可直接显示
商家代码:
# -*- conding:utf-8 -*-
# Author:OnlyZZQ菜鸟#商家添加商品界面
#欢迎界面
print("--------欢迎进入店铺商品信息系统----------")
#选择要咨询的操作
while 1:print("====请输入商品信息管理编号====")c_input = str(input("(1、添加;2、修改价格;q、退出):"))if c_input == "1":while 1:# 判断是否退出# 输入商品名称goods_add = input("请输入要添加的商品:")if (goods_add == "q" or goods_add == "Q"):print("欢迎再次使用")exit()else:isnum = Truewhile isnum:# 输入价格goods_pre = input("请输入价格:")if goods_pre.isdigit():goods_pre = str(goods_pre)isnum = Falseelse:print("输入有误,重新输入")# 追加列表sl = (" ".join([goods_add, goods_pre]))goods_list = open("gl.txt", "a")goods_list2 = goods_list.write(sl)goods_list2 = goods_list.write("\n")# goods_list2.close()# 显示当期菜单print("--------当前有如下商品------")goods_list = open("gl.txt", "r")goods_list2 = goods_list.read()print(goods_list2)print("----------------------------")goods_list.close()elif c_input == "2":print("--------当前有如下商品------")goods_list = open("gl.txt", "r")goods_list2 = goods_list.read()print(goods_list2)print("----------------------------")goods_list.close()#将文本里面的信息读取出来,并将列表转为字典g_list2 = goods_list2.splitlines()#再将列表解出来(使用空格切割出列表)gg_list2 = []for i in g_list2:gg_list2.append(i.split(" "))#print(gg_list2)#将列表转化为字典g_dict = dict(gg_list2)#print(g_dict)#提示输入要修改的商品价格while True:c_goods = input("请输入需要修改的商品名称:")if (c_goods in g_dict):c_price = input("请输入商品价格")g_dict[c_goods] = c_priceprint("修改成功!!!")#print(g_dict)#更新文件#清空c = open("gl.txt","w")c_file = c.write("")c.close()d = open("gl.txt","a")for i in g_dict:c_file = d.write(i)k = str(g_dict[i])c_file =d.write(" ")c_file = d.write(k)c_file =d.write("\n")d.close()elif (c_goods == "q" or c_goods == "Q"):print("欢迎再次使用")exit()else:print("输入有误,请重新输入")
客户端代码:
# -*- conding:utf-8 -*-
# Author:OnlyZZQ
#公众号:OnlyZZQ菜鸟
#列表列出商品信息'''
shoping_list = [["iphone","5000"],[ "ipad", "8000"],[ "mac", "12000"],[ "xiaomi", "1000"],[ "meizu", "2500"],[ "huawei", "3000"],[ "anycall", "4000"],["oppo", "2600"],]#将商品列表写如文件中
#将列表转为字符串for i in shoping_list:sl =(" ".join(["8、", "oppo", "2600"]))
goods_list = open("gl.txt","w")
goods_list.write(sl)
goods_list.close()
'''#储存用户选择列表
goods = []
#储存商品总金额列表
money = []t_money = 0#退出函数
'''
def getout():continue_shopping = input("是否继续购物(按Q退出购物)")if (continue_shopping == "q" or continue_shopping == "Q"):for i in money:t_money = t_money + int(i)print("您选择的商品为{_goods},金额为{_money}.余额为{_salay}".format(goods, str(t_money), salay))quit()'''
#从文件中读取商品列表
goods_str = open("gl.txt","r")
shoping_str = goods_str.read()
#print(shoping_str)
goods_str.close()
#将字符串转化为列表
#先按行转成列表
shoping_goods = shoping_str.splitlines()
#print(shoping_goods)
#按空格转换成列表
shoping_list = []
for i in shoping_goods:shoping_good = i.split(" ")shoping_list.append(shoping_good)
#print(shoping_list)
#存储用户购买记录信息
customer_b = open("cb.txt","r")
customer_buy = customer_b.read()
customer_b.close()
#显示用户购买记录
print(customer_buy)
#判断顾客金额
#先获取用户金额文件
customer_m = open("cm.txt","r")
customer_money = customer_m.read()
customer_m.close()
#判断里面是否有钱,没有的话,提示输入金额
if customer_money == "":print("这是空用户,请输入金额")is_money = Truewhile is_money:salay = input("请输入你的余额:")if (salay.isdigit()):salay = int(salay)is_money = Falseelse:print("您的输入不是余额,请从新输入")
elif int(customer_money) == 0:print("这是空用户,请输入金额")is_money = Truewhile is_money:salay = input("请输入你的余额:")if (salay.isdigit()):salay = int(salay)is_money = Falseelse:print("您的输入不是余额,请从新输入")
else:salay = int(customer_money)print("您目前账户余额为:", salay)while 1:#通过枚举获取下标for index,i in enumerate(shoping_list):print(index+1,i)#提示用户选择商品choose_in = input("请输入你要选择的商品")#判断输入是否为数字if choose_in.isdigit():choose = int(choose_in)# 判断商品是否存在if ((choose > shoping_list.__len__()) or (choose < 1)):print("你选择的商品不存在")continue# 出现金额不足elif (int(shoping_list[choose - 1][1]) > salay):print("余额不足,请重新选择或者退出")# getout()continueelse:print("你已经将%s加入购物车" % shoping_list[choose - 1][0])# 剩下余额salay = salay - int(shoping_list[choose - 1][1])# 显示加入购物车列表goods.append(shoping_list[choose - 1][0])# 金额money.append(shoping_list[choose - 1][1])print("你剩下的余额为", salay)continue#输入Q退出elif (choose_in == "q" or choose_in == "Q"):for l in money:t_money = int(t_money) + int(l)print("您选择的商品为", goods, "金额为{_money}.余额为{_salay}".format(_money=t_money, _salay=salay))#将用户余额写入文件中s_salay = str(salay)customer_m = open("cm.txt", "w")customer_money = customer_m.write(s_salay)customer_m.close()#将list转为字符串s_goods = " ".join(goods)#将购买金额转换为字符串tt_money = str(t_money)#将商品购买记录追加到文件中customer_b = open("cb.txt", "a")customer_buy = customer_b.write(s_goods)customer_buy = customer_b.write(" 总共花费:"+tt_money)customer_buy = customer_b.write("\n")customer_b.close()breakelse:print("输入有误,请重新输入:")continue
python自动化开发实战实战2:用列表与文件创建购物系统相关推荐
- Python自动化开发基础 项目实战-监控系统 Day16-李杰-专题视频课程
Python自动化开发基础 项目实战-监控系统 Day16-14487人已学习 课程介绍 课程特色:实战,细致,生动,深入浅出 适用人群:适合Python初学者 课程详情:从0基础开始 ...
- Python自动化开发基础 分布式监控系统开发实战 day8-李杰-专题视频课程
Python自动化开发基础 分布式监控系统开发实战 day8-10994人已学习 课程介绍 从0基础开始讲解Python语法.流程控制.函数式编程.面向对象开发等重要的Python开发 ...
- 手把手教你写网站:Python WEB开发技术实战
摘要:本文详细介绍了Python WEB开发的基础入门.以一个博客站点的开发为例讲解了基于Django框架开发WEB站点的全过程.通过本文的学习可以快速掌握基于Django的Python WEB的开发 ...
- Python自动化开发【1】:Python简介和入门
Python自动化开发之路 [第1篇]:Python简介和入门 编程与编程语言 一 编程与编程语言python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语 ...
- 老男孩Python自动化开发12期完整版(含作业代码课件)
老男孩Python自动化开发12期完整版(含作业代码课件) 链接:https://pan.baidu.com/s/1zCKn_VjstbCfTYCHCBKZgQ 密码:zujz 转载于:https:/ ...
- 最新《python自动化开发网络班项目实战》
课程简介: 男孩Python运维开发课程,目前国内最早的且唯一真正的运维开发课程培训,带领学生开发固定资产-批量管理-日志审计等功能于一身的python运维软件产品,还有bbs,web聊天室等产品.极 ...
- pythonweb开发-手把手教你写网站:Python WEB开发技术实战
摘要:本文详细介绍了Python WEB开发的基础入门.以一个博客站点的开发为例讲解了基于Django框架开发WEB站点的全过程.通过本文的学习可以快速掌握基于Django的Python WEB的开发 ...
- python web-手把手教你写网站:Python WEB开发技术实战
摘要:本文详细介绍了Python WEB开发的基础入门.以一个博客站点的开发为例讲解了基于Django框架开发WEB站点的全过程.通过本文的学习可以快速掌握基于Django的Python WEB的开发 ...
- python开发一个自己的技术网站_手把手教你写网站:Python WEB开发技术实战
摘要:本文详细介绍了Python WEB开发的基础入门.以一个博客站点的开发为例讲解了基于Django框架开发WEB站点的全过程.通过本文的学习可以快速掌握基于Django的Python WEB的开发 ...
最新文章
- 三维点云分割综述(下)
- 操作系统的八股文自述(持续更新)
- 影院平台搭建 - (2)FLV发布系统的简单搭建
- leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)
- uva 1451——Average
- python traceback 丢失_基于python traceback实现异常的获取与处理
- 学计算机专业体面吗,2021女生学计算机专业好吗 前景怎么样
- HDU - 1272小希的迷宫
- matlab堆栈的思想,[转载]Matlab源代码:堆栈类Stack的实现
- php获取手机目录,PHP通过API获取手机号码归属地
- Windows中安装Electron说明
- MUI Picker选择器 自定义省市地址三级联动
- 热力地图高德_《高德地图》城市热力图开启查看方法
- Leetcode592. 分数加减
- Generative Adversarial Networks overview(1)
- can是什么时候处于显性_CAN总线什么情况下为空闲状态? - 全文
- APP、网站等注册、登录、改密等发送验证码短信的接入流程---创蓝253云通讯
- 剑指offer | 面试题10:斐波那切数列
- OpenCV-Python学习(19)—— OpenCV 图像几何变换之图像缩放(cv.warpAffine、cv.resize)
- 学计算机如何防辐射,电脑辐射危害大 五妙招正确防辐射