请设计实现一个商城系统开发v2.0【代码优化】
2024-08-24 21:30:07
#!/usr/bin/env python 优化的部分:1.改用字典取键,来调用函数【原来是用if-else判断】 【补充】:也可以用列表,按索引取,可以在列表最前面加一个“”任意元素,凑成一个。就和缩影对上了
# -*- coding:utf-8 -*-
2.直接return 不再 return func(),这样写不好,返回值是None。改完以后 return 执行直接跳回while循环
3.返回上一级改成 用if 判断 + continue 增加了输入其他内容报 “输入有误”
def goods_list(): 所有函数功能调用都在父级函数下的while循环里调用的
'''
# 查看商品列表
:return:
'''
print("欢迎使用老子的购物商城【商品管理】【查看商品列表】".center(30, "*"))
with open("goods.txt",mode="r",encoding="utf-8")as file: #读取所有商品
content=file.read()
content=content.strip().split("\n")
total_lengh=len(content)
# 分页展示
pages,remainder=divmod(total_lengh,10)
if remainder>0:
pages+=1
while True:
option=input("请输入要看的页数(输入N返回上一级:):")
if option.upper() == "N":
return
if int(option)<0 or int(option)>pages:
print("页码不输入有误")
continue
x=(int(option)-1)*10
y=int(option)*10
print("这是第%s页"%option)
for i in content[x:y]:
print(i) def goods_search():
'''
# 根据关键字模糊搜索指定商品 :return:
''' print("欢迎使用老子的购物商城【商品管理】【根据关键字搜索】".center(30, "*"))
with open("goods.txt", mode="r", encoding="utf-8")as file:
content = file.read()
content_list = content.strip().split("\n")
while True:
key_word = input("请输入要查询的关键字(输入N返回上一级:):")
for i in content_list:
if key_word in i:
print(i)
if key_word.upper() == "N":
return
def goods_record():
'''
# 录入商品
:return:
'''
print("欢迎使用老子的购物商城【商品管理】【录入商品】".center(30, "*"))
while True:
info = []
with open("goods.txt", mode="a", encoding="utf-8")as file:
good_name = input("请输入商品名称(输入N返回上一级:):")
if good_name.upper() =="N":
return
info.append(good_name)
good_price = input("请输入商品价格):")
info.append(good_price)
good_count= input("请输入商品数量:")
info.append(good_count)
file.write(" ".join(info)+"\n")
print("添加成功") def goods_manage():
'''
查看商品管理目录
:return:
'''
goods_manage_list={"":goods_list,"":goods_search,"":goods_record,"N":function_list}
while True:
print("欢迎使用老子的购物商城【商品管理】".center(30, "*"))
print('''
1.查看商品列表
2.根据关键字搜索指定商品
3.录入商品
''')
num1=input("请选择(输入N返回上一级:)")
if not goods_manage_list.get(num1):
print("输入有误,请重新输入")
continue
goods_manage_list.get(num1)() #调用功能 结束以后 继续返回执行 while循环 def vip_manage():
'''
#会员管理
:return:
''' print("功能正在开发中哦,亲。请重新选择\n") def function_list():
'''
#总功能列表
:return:
'''
function_list_dict={"":goods_manage,"":vip_manage,"N":function_list,'e':exit} #输入 e 直接退出程序
while True:
print("欢迎使用老子的购物商城".center(30, "*"))
print(" 1.商品管理 2.会员管理(不可选,正在开发中)")
num=input("请选择(输入N返回上一级:)")
if not function_list_dict.get(num):
print("输入有误,请重新输入")
continue
function_list_dict.get(num)() function_list()
最新文章
- style设置/获取样式的问题 和 offsetWidth/offsetHeight的问题
- display:none与visibility:hidden区别
- 维吉尼亚密码java代码实现根据密钥长度计算IC值过程
- 前端模块化——seaJS
- FileProvider解决FileUriExposedException
- LeetCode第六天
- python并发编程之协程知识点
- STM32的SWD调试进不了main函数
- eolinker——添加项目成员
- 洛谷P5219 无聊的水题 I [prufer序列,生成函数,NTT]
- MYSQL数据模型
- 离线部署 pm2
- WPF触发器(Trigger) - DataTrigger
- scenario testing
- 05 爬虫之scrapy
- Java DB 访问(三)mybatis mapper interface接口
- 什么是lib文件,lib和dll的关系如何
- IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)
- 获取列表数据时,getList 设置默认参数:getList(page = 1),点击分页及前往时,传page参数,其他使用page的默认值:1
- springboot开篇 (一)简单邮件发送
热门文章
- MAVEN学习总结1
- 【poj1679】The Unique MST
- 一段上传图片预览JS脚本,Input file图片预览的实现
- Hyperledger Fabric Transaction Proposal过程
- 数字图像处理实验(10):PROJECT 05-01 [Multiple Uses],Noise Generators 标签: 图像处理MATLAB 2017-05-26 23:36
- hdu 4279 Number(G++提交)
- AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)
- POJ - 2965 The Pilots Brothers&#39; refrigerator(压位+bfs)
- 企业管理系统——第三周需求&;原型改进_张正浩,黄锐斌
- SharePoint 2013备份方法整理