python3实现名片管理系统(文件版)

def menu():
    #首先定义功能列表函数menu()
    print("    名片管理系统  V1.0  ")
    print("1:增加新用户")
    print("2:删除新用户")
    print("3:修改用户名")
    print("4:查询用户名")
    print("5:显示所有用户")
    print("6:保存到文件")
    print("7:退出本程序")
# 定义空列表存储用户信息
users = []
def add():
    #定义追加新用户信息函数
    new_info = {}
    new_info['name'] = input("请输入你要增加的姓名:\n")
    new_info['QQ'] = input("请输入你要增加的QQ:\n")
    new_info["weixin"] = input("请输入你要增加的微信:\n")
    new_info["address"] = input("请输入你要增加的住址:\n")
    # 使用append方法追加字典元素到列表
    users.append(new_info)
    print("\n")
    # users.append(name)
    print(users)
#使用死循环 让程序不自动停止
def deluser():
    name = input("请输入你要删除的姓名:\n")
    count = 1
    # if name in users.name:
    for temp in users:
        if temp['name'] == name:
            count += 1
            users.remove(temp)
            print("删除后的列表为:%s" % users)
            break
    if (count == 1):
        print("没有该用户")
def  update():
    count = 1
    name = input("请输入你要更改的姓名:\n")
    for temp in users:
        if temp['name'] == name:
            count += 1
            after_name = input("请输入你更改后的名字\n")
            after_qq = input("请输入你更改后的QQ\n")
            after_weixin = input("请输入你更改后的名微信\n")
            after_address = input("请输入你更改后的地址\n")
            temp['name'] = after_name
            temp['QQ'] = after_qq
            temp['weixin'] = after_weixin
            temp['address'] = after_address
            print("更改后的列表为:%s" % users)
    if count == 1:
        print("没有该用户。")
def findName():
    count = 1
    find_name = input("请输入查询的名字")
    for temp in users:
        if temp['name'] == find_name:
            count += 1
            print("姓名:%s  QQ:%s 微信:%s 住址:%s"%(temp['name'],temp['QQ'],temp['weixin'],temp['address']))
    if count == 1:
        print("么有该用户")
def show():
    print("姓名\tQQ\t微信\t住址\t")
    for temp in users:
        print("%s\t %s\t%s\t %s" % (temp['name'], temp['QQ'], temp['weixin'], temp['address']))
def saveToFile():
    f = open("userinfo.data","w")
    f.write(str(users))
    f.close()
def loadFile():
    global  users
    try:
        f = open("userinfo.data","r")
        users = eval(f.read())
        f.close()
    except Exception:
        pass


def main():
    loadFile()
    menu()
    while 1:
        num = int(input("请输入操作序号:\n"))
        if num == 1:  # 追加新用户信息
            add()
        elif num == 2:  # 删除指定指定用户信息
            deluser()
        elif num == 3:  # 更改指定用户信息
            update()
        elif num == 4:    #查询指定用户信息
             findName()
        elif num == 5:  # 展示所有用户信息
            show()
        elif num ==6:
            saveToFile()
        elif num == 7:
            break
if __name__ == "__main__":
    main()

猜你喜欢

转载自blog.csdn.net/peng_for_csdn/article/details/88685559