Python超市商品管理系统

系统需要用户先登录,再进行操作,其中包含一下功能菜单

1、显示商品列表

2、增加商品信息

3、删除商品

4、设置商品折扣

5、修改商品价格信息

6、退出

a、使用列表嵌套字典的方式保存用户数据(包含用户名、密码、姓名);

b、使用列表嵌套字典的方式保存商品数据(包含编号、名称、价格、折扣);

c、编写用户登录的函数,返回登录结果

d、循环提示菜单,业务完毕时返回主菜单,退出时回到登录页面;

e、将功能菜单中的业务功能各自编写到函数中

f、用户选择不同业务编号时,调用已经

python 复制代码
# -*- coding:utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
# a、使用列表嵌套字典的方式保存用户数据(包含用户名、密码、姓名)
user1 = {"用户名": "111", "密码": "123", "姓名": "刘皇叔"}
user2 = {"用户名": "222", "密码": "123", "姓名": "李狗蛋"}
userlist = [user1, user2]

# b、使用列表嵌套字典的方式保存商品数据(包含编号、名称、价格、折扣)
commodity1 = {"编号": "1001", "名称": "苹果", "价格": 8, "折扣": 1}
commodity2 = {"编号": "1002", "名称": "香蕉", "价格": 6, "折扣": 1}
commodity3 = {"编号": "1003", "名称": "西瓜", "价格": 5, "折扣": 1}
commoditylist = [commodity1, commodity2, commodity3]


# 登录
def login():
    msg = "失败"
    count = 0
    while True:
        uname = input("请输入账号:")
        upwd = input("请输入密码:")
        for user in userlist:
            if uname == user["用户名"] and upwd == user["密码"]:
                print("登录成功,欢迎你", user["姓名"])
                msg = "成功"
                break
        if msg == "失败":
            count += 1
            if count < 3:
                print("用户名密码错误!请重新登录", "输入第", count, "次")
            else:
                print("用户已锁定!")
                break
        else:
            break
    return msg


# 1、显示商品列表
def showProduct():
    print("----------产品信息----------")
    print("-编号----名称----价格----折扣-")
    for commodity in commoditylist:
        print("-" + commodity["编号"] + "----" + commodity["名称"] + "-----" + str(commodity["价格"]) + "-----" + str(
            commodity["折扣"]))
    print("----------------------------")


# 2、增加商品信息
def addProduct():
    list1 = []
    for num in commoditylist:
        list1.append(int(num["编号"]))
    num = str(max(list1) + 1)
    print("----------添加商品信息----------")
    mc = input("请输入产品名称:")
    jg = float(input("请输入产品价格:"))
    zk = 1
    newProduct = {"编号": num, "名称": mc, "价格": jg, "折扣": zk}
    commoditylist.append(newProduct)
    print("商品" + mc + "添加成功")
    print("-------------------------------")
    showProduct()


# 3、删除商品
def delproduct():
    showProduct()
    while True:
        msg = 0
        num = input("请输入要删除商品的编号")
        for product in commoditylist:
            if num == product["编号"]:
                print("商品", product["名称"], "正在删除")
                commoditylist.remove(product)
                print("删除成功!")
                msg = 1
                break
        if msg == 0:
            print("输入的产品编号不正确,请重新输入")
            jx = input("取消输入请按1,继续请按2")
            if jx == 1:
                break
            elif jx == 2:
                continue
            else:
                print("输入错误请重新输入")
        else:
            showProduct()
            break


# 4、设置商品折扣
def setDiscount():
    while True:
        mag = 0
        name = input("请输入要设置折扣的商品名称")
        for x in commoditylist:
            if name == x["名称"]:
                zk = float(input("请输入要设置产品的折扣(0.1-1)"))
                x["折扣"] = zk
                print(x["名称"] + "的折扣为:" + str(zk))
                mag = 1
                break
        if mag == 0:
            print("输入的商品名称不存在,请重新输入")
            jx = input("取消输入请按1,继续请按2")
            if jx == "1":
                break
            elif jx == "2":
                continue
            else:
                print("输入错误请重新输入")
        else:
            showProduct()
            break


# 5、修改商品价格信息
def setPrice():
    while True:
        mag = 0
        num = input("请输入要设置价格的商品编号")
        for x in commoditylist:
            if num == x["编号"]:
                jg = float(input("请输入要设置产品价格"))
                x["价格"] = jg
                print(x["名称"] + "的价格为:" + str(jg))
                mag = 1
                break
        if mag == 0:
            print("输入的商品编号不存在,请重新输入")
            jx = input("取消输入请按1,继续请按2")
            if jx == "1":
                break
            elif jx == "2":
                continue
            else:
                print("输入错误请重新输入")
        else:
            showProduct()
            break


# 6、根据价格排序显示商品列表
def sort():
    choice = int(input("请选择升序或者降序(1、升序 2、降序)"))
    clist = []
    for commodity in commoditylist:
        clist.append(commodity["价格"])
    clist = list(set(clist))
    if choice == 1:
        newlist = sorted(clist)
        for price in newlist:
            for product in commoditylist:
                if price == product["价格"]:
                    print("-" + product["编号"] + "----" + product["名称"] + "-----" + str(product["价格"]) + "-----" + str(
                        product["折扣"]))
    else:
        newlist = sorted(clist, reverse=True)
        for price in newlist:
            for product in commoditylist:
                if price == product["价格"]:
                    print("-" + product["编号"] + "----" + product["名称"] + "-----" + str(product["价格"]) + "-----" + str(
                        product["折扣"]))


# 主程序开始
while True:
    result = login()
    if result == "成功":
        while True:
            print("*********主菜单*********")
            print("1、显示商品列表")
            print("2、增加商品信息")
            print("3、删除商品")
            print("4、设置商品折扣")
            print("5、修改商品信息")
            print("6、根据价格排序商品")
            print("7、退出")
            print("*********************")
            choice = int(input("请输入您的选项(1-7)"))
            if choice == 1:
                showProduct()
            elif choice == 2:
                addProduct()
            elif choice == 3:
                delproduct()
            elif choice == 4:
                setDiscount()
            elif choice == 5:
                setPrice()
            elif choice == 6:
                sort()
            elif choice == 7:
                print("------------系统已退出")
                break
            else:
                print("没有此功能请重新输入")
                continue

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

相关推荐
你好潘先生13 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师13 小时前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码13 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf13 小时前
FastAPI 如何连接 MySQL
后端·python
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780511 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent1 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
SelectDB2 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码3 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python