员工管理系统

2023-02-28  本文已影响0人  晨颜

正确代码在最后
错误代码如下:

source_data='123|123'
suser=source_data.split('|')[0]
spwd=source_data.split('|')[1]
# print(suser,spwd)
for i in range(3):
    print("输入用户名:")
    user=input().strip()
    print("输入密码:")
    pwd=input().strip()
    if(user==suser and pwd==spwd):
        print("成功")
        cmd="w"
        employee_dict=[]
        count=0
        while cmd != '0' :
            detaile_dicct={}
            print("输入指令:(1增加 2查看 3删除 0退出)")
            cmd = input().strip()
            if cmd == '1':
                print("请输入编号")
                num=input().strip()
                if num not in employee_dict:
                    #detaile_dicct[bh]=num
                    detaile_dicct.update({'num': num, 'age': 20})
                    print("请输入姓名")
                    name = input().strip()
                    detaile_dicct.update({'num': num, 'name': name})
                    employee_dict[count] = detaile_dicct
                    count +=1
                else:
                    print("已经存在")
            elif cmd == '2':
                print(employee_dict)
            elif cmd == '3':
                print("请输入编号")
                num = input().strip()
                if num in employee_dict:
                    # detaile_dicct[bh]=num
                    aa=employee_dict.remove(num)
                    print(employee_dict)
                else:
                    print("不存在")

            elif cmd == '0':
                break
            else:
                print("错误,重来")
            print("成功")
        break
    else:
        print("错误,重来")
else:
    print("失败,已锁定")

解决方法将28行改为employee_dict.append(detaile_dicct)
下一个问题
判断员工编号功能无法实现


image.png

解决方法,从employee_dict列表一个一个取出元组
定义一个临时存储空间,防止新数据覆盖原内容
现代码如下

source_data='|'
suser=source_data.split('|')[0]
spwd=source_data.split('|')[1]
# print(suser,spwd)
for i in range(3):
    print("输入用户名:")
    user=input().strip()
    print("输入密码:")
    pwd=input().strip()
    if(user==suser and pwd==spwd):
        print("成功")
        cmd="w"
        employee_dict=[{'num': '1', 'name': '1'}, {'num': '2', 'name': '2'}]
        count=0
        while cmd != '0' :
            detaile_dicct={}
            print("输入指令:(1增加 2查看 3删除 0退出)")
            cmd = input().strip()
            if cmd == '1':
                print("请输入编号")
                num=input().strip()
                for detaile_dicct in employee_dict:
                    print(detaile_dicct)
                    if num ==detaile_dicct.get('num'):
                        print("已经存在")
                        break
                else:
                    detaile_dicct_tmp={}#添加临时存储空间
                    print(employee_dict)
                    #detaile_dicct[bh]=num
                    #detaile_dicct.update({'num': num})
                    print("请输入姓名")
                    name = input().strip()
                    detaile_dicct_tmp.update({'num': num, 'name': name})#将值给临时存储空间
                    print(employee_dict)
                    print(detaile_dicct)
                    # employee_dict[count] = detaile_dicct
                    employee_dict.append(detaile_dicct_tmp)#将临时存储空间的内容给employee_dict
                    count +=1
                    print(employee_dict)

            elif cmd == '2':
                print(employee_dict)
            elif cmd == '3':
                print("请输入编号")
                num = input().strip()
                for detaile_dicct in employee_dict:
                    print(detaile_dicct)
                    if num ==detaile_dicct.get('num'):
                        print("存在,执行删除")
                        # detaile_dicct[bh]=num
                        aa=employee_dict.remove(num)
                        print(employee_dict)
                else:
                    print("不存在")

            elif cmd == '0':
                break
            else:
                print("错误,重来")
            #print("成功")
        break
    else:
        print("错误,重来")
else:
    print("失败,已锁定")
删除存在bug image.png

正确代码如下

source_data='|'
suser=source_data.split('|')[0]
spwd=source_data.split('|')[1]
# print(suser,spwd)
for i in range(3):
    print("输入用户名:")
    user=input().strip()
    print("输入密码:")
    pwd=input().strip()
    if(user==suser and pwd==spwd):
        print("成功")
        cmd="w"
        employee_dict=[{'num': '1', 'name': '1'}, {'num': '2', 'name': '2'}]
        count=0
        while cmd != '0' :
            detaile_dicct={}
            print("输入指令:(1增加 2查看 3删除 0退出)")
            cmd = input().strip()
            if cmd == '1':
                print("请输入编号")
                num=input().strip()
                for detaile_dicct in employee_dict:
                    print(detaile_dicct)
                    if num ==detaile_dicct.get('num'):
                        print("已经存在")
                        break
                else:
                    detaile_dicct_tmp={}#添加临时存储空间
                    print(employee_dict)
                    #detaile_dicct[bh]=num
                    #detaile_dicct.update({'num': num})
                    print("请输入姓名")
                    name = input().strip()
                    detaile_dicct_tmp.update({'num': num, 'name': name})#将值给临时存储空间
                    print(employee_dict)
                    print(detaile_dicct)
                    # employee_dict[count] = detaile_dicct
                    employee_dict.append(detaile_dicct_tmp)#将临时存储空间的内容给employee_dict
                    count +=1
                    print(employee_dict)

            elif cmd == '2':
                print(employee_dict)
            elif cmd == '3':
                print("请输入编号")
                num = input().strip()
                for detaile_dicct in employee_dict:
                    print(detaile_dicct)
                    if num ==detaile_dicct.get('num'):
                        print("存在,执行删除")
                        
                        # detaile_dicct[bh]=num
                        #aa=employee_dict.remove(num)
                        
                        print(aa)
                        employee_dict.remove(detaile_dicct)#移除employee_dict列表中当前字典
                        print(employee_dict)
                        break
                else:
                    print("不存在")

            elif cmd == '0':
                break
            else:
                print("错误,重来")
            #print("成功")
        break
    else:
        print("错误,重来")
else:
    print("失败,已锁定")
上一篇 下一篇

猜你喜欢

热点阅读