天天看點

python用dict實作使用者的CURD

'''
list,dict實作對 user crud操作
需求如下:

  1 界面清單
   歡迎來到使用者管理系統
   1 添加使用者
   2 删除使用者
   3 修改使用者
   4 查詢使用者
   5 查詢全部
   6 退出
  2 删除,修改,查詢,分别根據id來操作
  3 沒有删除,修改,查詢的項,給出相應的提示,操作的結果給出相提示
  4 初始化3個使用者
  5 使用者有屬性id,name,pw,一個使用者代表一個字典,然後放到list中


user1={'id':2,'name':'張三1','pw':'123'}
user2={'id':23,'name':'張三2','pw':'123'}
user3={'id':21,'name':'張三1','pw':'123'}
users = [user1,user2,user3]
user = users[1]
user['name']

numbers =[1,3,21]
numbers[0]


if
while
list
變量
輸入輸出


1 實作菜單
2 實作CRUD
'''
user1={'id':21,'name':'張三1','pw':'123'}
user2={'id':22,'name':'張三2','pw':'123'}
user3={'id':23,'name':'張三3','pw':'123'}
users = [user1,user2,user3]
temp = []
# for i in users:
#     temp.append(i['id'])
print('''
歡迎來到使用者管理系統
   1 添加使用者
   2 删除使用者
   3 修改使用者
   4 查詢使用者
   5 查詢全部
   6 退出
''')
flag = 'y'
while flag == 'y':
    a = int(input('請選擇:'))
    if a == 1:
        while 1:
            no = int(input('請輸入你要添加的使用者id:'))
            for i in users:
                temp.append(i['id'])
            s = set(temp)
            if no in s:
                print('使用者已存在,請重新輸入')
            else:
                name = input('請輸入您要添加的使用者姓名:')
                pw = input('請輸入您要添加的使用者密碼:')
                user4={}
                user4['id'] = no
                user4['name'] = name
                user4['pw'] = pw
                users.append(user4)
                for j in users:
                    temp.append(j['id'])
                s = set(temp)
                print(users)
                print('添加成功')
                print('目前存在的id:',s)
                break
    elif a == 2:
        while 1:
            no = int(input('請輸入你要删除的使用者id:'))
            for i in users:
                temp.append(i['id'])
            if no not in temp:
                print('使用者不存在,請重新輸入')
                continue
            else:
                for i in users:
                    if i['id'] == no:
                        users.remove(i)
                        print(users)
                        print('删除成功')
                        break
            break
    elif a == 3:
        while 1:
            no = int(input('請輸入你要修改的使用者id:'))
            for i in users:
                temp.append(i['id'])
            if no not in temp:
                print('使用者不存在,請重新輸入')
                continue
            else:
                name = input('請輸入您要修改的使用者姓名:')
                pw = input('請輸入您要修改的使用者密碼:')
                for i in users:
                    if i['id'] == no:
                        i['name'] = name
                        i['pw'] = pw
                        print(i)
                        print('修改成功')
                        break
            break
    elif a == 4:
        while 1:
            no = int(input('請輸入你要查詢的使用者id:'))
            for i in users:
                temp.append(i['id'])
            if no not in temp:
                print('使用者不存在,請重新輸入')
                continue
            else:
                for i in users:
                    if i['id'] == no:
                        print(i)
                        break
            break
    elif a == 5:
        print(users)
    elif a == 6:
        a = input('點選enter退出')
        break
    else:
        print('輸入有誤,請重新輸入')
    flag = input('是否繼續(y/n):')