Python 用户名 密码 的字典列表的增删查改操作示例

本文主要介绍在Ubuntu下使用python3写的用列表里面字典的方式来实现用户名和密码的增删查改操作的示例。

#!/usr/bin/env python3
# coding: utf-8

users = [{username:shizengfeng, password:12345},
         {username:xiaoming, password:12345},
         {username:Jim, password:12345}]

def addUser(name, pwd):
    userDict = {}
    userDict[username] = name
    userDict[password] = pwd
    users.append(userDict)

def delUser(name):
    for line in users:
        if (line[username] == name):
            users.remove(line)
            print(delete +name+ successfully)
            break

def judgeUser(name):
    for line in users:
        if line[username] == name:
            return True
    return False

def modifyUser(oldname, newname, pwd):
    for line in users:
        if line[username] == oldname:
            line[username] = newname
            line[password] = pwd


print(users)
delUser(shizengfeng)
print(users)
addUser(Lucy, 999)
print(users)
print(judgeUser(xiaoming))
print(judgeUser(xiao))
modifyUser(Jim, Kite, 110)
print(users)

运行后,结果如下

经验分享 程序员 微信小程序 职场和发展