您的当前位置:首页正文

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

来源:爱站旅游
导读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)

运行后,结果如下

因篇幅问题不能全部显示,请点此查看更多更全内容

Top