复习Python第五节-->综合训练-商品管理系统(基础版)

大宝的超市开业了,为了更好的管理商品信息,准备开发一个商品管理系统。系统需要用户先登录,再进行操作,其中包含以下功能菜单:


       1.显示商品列表

        2.增加商品信息

        3.删除商品

        4.设置商品折扣

        5.修改商品信息

        6.退出

a.使用列表嵌套字典的方式保存用户数据也含用户名,密码,姓名)﹔

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

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

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

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

f.用户选择不同业务编号时,调用已经写好的各种函数。

答案:

user1 = {"用户名": "aaa", "密码": "123", "姓名": "张三"}
user2 = {"用户名": "bbb", "密码": "123", "姓名": "李四"}
user3 = {"用户名": "ccc", "密码": "123", "姓名": "王五"}
usersList = [user1, user2, user3]

p1 = {"编号": "1001", "名称": "萃果", "价格": 5, "折扣": 1}
p2 = {"编号": "1002", "名称": "香蕉", "价格": 3, "折扣": 1}
p3 = {"编号": "1003", "名称": "牛奶", "价格": 3, "折扣": 1}
p4 = {"编号": "1004", "名称": "白菜", "价格": 1.5, "折扣": 1}
p5 = {"编号": "1005", "名称": "西瓜", "价格": 3, "折扣": 1}
productsList = [p1, p2, p3, p4, p5]  # 商品列表
#登录
def login():
    msg="失败"
    while 1==1:
        username=input("请输入用户名:")
        userpass=input("请输入密码:")
        for x in usersList:
            if username==x["用户名"] and userpass==x["密码"]:
                print("---------验证成功!欢迎你!")
                msg="成功"
                break
        if msg=="失败":
            print("用户名密码错误,请重新输入")
            continue
        else:
            break
    return msg         #登录的结果
#1。显示商品列表
def showProcucts():
    print("编号 -----名称-----价格-----折扣")
    for product in productsList:
        print(product["编号"] + "------" + product["名称"] + "------" + str(product["价格"])+"------" +str(product["折扣"]))
    print("-----------------------------")
#2.增加商品信息
def addProcucts():
    lista=[]
    for product in productsList:
        lista.append(int(product["编号"]))
        newNum=str(1001+len(productsList))
        name=input("请输入商品名称:")
        price=float(input("请输入商品价格:"))
        newProduct={"编号": newNum, "名称": name, "价格": price, "折扣": 1}
        productsList.append(newProduct)
        print("添加成功")
        showProcucts()
#3.删除商品
def  delProcucts():
    while 1==1:
        msg=0 #记录商品是否存在
        num=input("请输入要删除的商品编号:")
        for product in productsList:
            if num==product["编号"]:
                print("---正在删除",product["名称"],"商品")
                productsList.remove(product)
                print("---删除成功")
                msg=1
                break
        if msg==0:
            print("商品编号不存在!")
            choice=int(input("取消请按1,重新输入请按2:"))
            if choice==1:
                break
            else:
                continue
        else:
            showProcucts()
            break
#4.设置商品折扣
def setdiscount():
    while 1==1:
        msg=0  #记录商品是否存在
        new1=input("请输入商品编号:")
        for product in productsList:
            num2=product["编号"]
            if new1==num2:
                newdiscount=float(input("请输入新的折扣:"))
                product["折扣"]=newdiscount
                print("---商品", product["名称"], "折扣已设置成功,", newdiscount * 10, "折! ")
                msg=1
                break
        if msg==0:
            print("商品不存在!")
            choice = int(input("取消请按1,重新输入请按2:"))
            if choice == 1:
                break
            else:
                continue
        else:
            showProcucts()
            break
#5.修改商品信息
def setProcucts():
    while 1==1:
        msg=0  #记录商品是否存在
        new1=input("请输入商品编号:")
        for product in productsList:
            num2=product["编号"]
            if new1==num2:
                newprice=float(input("请输入新的价格:"))
                product["价格"]=newprice
                print("---商品", product["名称"], "价格已设置成功,新价格位:", newprice)
                msg=1
                break
        if msg==0:
            print("商品不存在!")
            choice = int(input("取消请按1,重新输入请按2:"))
            if choice == 1:
                break
            else:
                continue
        else:
            showProcucts()
            break
#按价格进行排序
def sorted1():
    choice=int(input("请输入升序或者降序(1.代表升序,2.代表降序)"))
    plist=[]
    for product in productsList:
        plist.append(product["价格"])
        plist = list(set(plist))  #去重思想
    if choice == 1:
        newList=sorted(plist)
        for price in newList:
            for product in productsList:
                if price==product["价格"]:
                    print(product)
    elif choice==2:
        newList=sorted(plist,reverse=True)
        for price in newList:
            for product in productsList:
                if price == product["价格"]:
                    print(product)
    else:
        pass
if __name__ =='__main__':
     while 0==0:
        msg=0
        result=login()
        if result=="成功":
            while 2 == 2:
                print("-------------主菜单 -----------------")
                print("------1显示商品列表")
                print("------2.增加商品信息")
                print("------3.删除商品")
                print("------4.设置商品折扣")
                print("------5.修改商品信息")
                print("------6.商品排序")
                print("------7.退出")
                choice=int (input("请选择业务编号(1-7)"))
                if choice==1:
                    showProcucts()
                elif choice == 2:
                    addProcucts()
                elif choice == 3:
                    delProcucts()
                elif choice == 4:
                    setdiscount()
                elif choice == 5:
                    setProcucts()
                elif choice ==6:
                    sorted1()
                elif choice==7:
                    print("-------正在退出------")
                    msg=1
                    break;
                else:
                    print("没有此功能,请重新选择")
                    continue
        if msg==1:
            break

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值