不多说,直接上完整代码

可以直接复制去运行

注意:

用户名:root

密码:000000

import time
import sys
import os

#列表存放字典
list = []

def tuichu():
    try:
        print("退出中")
        sj()
        print("\n退出成功")
        print("\n谢谢使用本系统!!!")
        sys.exit(0)
    except Exception:
        print("出错")

def sj():
    # 进度条功能
    import time
    num = 0
    for i in range(11):
        # 打印进度条
        print("\r%s" % num, "%", "■ " * i, end="", flush=True)
        num += 10
        # 登录缓冲速度
        time.sleep(0.15)

print("*******欢迎登陆学生通讯管理系统 v4.0*******")
count = 3
while count >= 1:
    root = "root"
    mima = 000000
    count -= 1
    root1 = input("****请输入账号:")
    try:
        mima1 = int(input("****请输入密码:"))
    except Exception:
        print("请输入数字!")
    if root1 == root and mima1 == mima:
        print("登陆中")
        sj()
        print("\n登陆成功")
        break
    else:
        if count > 0:
            print("输入错误,你还有%s次机会,请重新输入!" % count)
        else:
            print("达到最大输入错误次数,系统自动退出。。。")
            tuichu()

def xitong():
    print("-" * 30)
    print("     学生通讯录管理系统   v4.0")
    print("1. 添加学生")
    print("2. 删除学生")
    print("3. 修改学生")
    print("4. 查询学生")
    print("5. 获取所有学生通讯录信息")
    print("6. 删除所有学生通讯录信息")
    print("7. 保存信息")
    print("8. 清理信息")
    print("9. 退出系统")
    print("-" * 30)

def shuru():
    try:
        shu = int(input("请输入选择的序号:"))
        return shu
    except Exception:
        print("请输入正确序号!!!")

#存放位置
dict = {
    # "张三": {"性别": "男", "电话": "17812234231"},
    # "李丽": {"性别": "女", "电话": "17212314123"},
    # "王五": {"性别": "男", "电话": "18234569091"},
}

def addname():
    try:
        name = str(input("请输入姓名:"))
    except Exception:
        print("请输入正确姓名")

    # 检验性别正确性
    # 计算输入次数
    p = 3
    while p >= 0:
        try:
            sex = str(input("请输入性别:"))
        except Exception:
            print("请输入正确性别!")
        # 判断是否为男/女
        if sex == '男' or sex == '女':
            break
        else:
            if p > 0:
                print("请输入正确性别! 你还有%s次输入机会" % p)
                p -= 1
            else:
                print("达到最大输入次数,系统自动退出!")
                tuichu()

    # 检验电话号码正确姓
    # 计算输入次数
    t = 3
    while t >= 0:
        try:
            telphone = input("请输入联系电话:")
            if telphone.isdigit():
                if len(telphone) == 11:
                    # 将输入的信息加入字典中
                    dict["姓名:"] = name
                    dict["性别:"] = sex
                    dict["电话:"] = telphone
                    list.append(dict)
                    print("添加中")
                    sj()
                    print("\n添加成功")
                    break
                else:
                    if t > 0:
                        print("请输入正确电话号码! 你还有%s次输入机会" % t)
                        t -= 1
                    else:
                        print("达到最大输入次数,系统自动退出!")
                        tuichu()
            else:
                print("请输入数字!")
        except Exception:
            print("请输入正确数字")

def deldict():
    try:
        name = input("请输入你要删除的姓名:")
        del dict[name]
        print("执行开始")
        sj()
        print("\n执行成功")
    except Exception:
        print("未找到此人,请重新输入")

def xiugai():
    try:
        name = input("请输入要修改的姓名:")
        # 检验性别正确性
        # 计算输入次数
        p = 3
        while p >= 0:
            try:
                sex = str(input("请输入要修改的性别:"))
            except Exception:
                print("请输入正确性别!")
                p -= 1
            # 判断是否为男/女
            if sex == '男' or sex == '女':
                break
            else:
                if p > 0:
                    print("请输入正确性别! 你还有%s次输入机会" % p)
                else:
                    print("达到最大输入次数,系统自动退出!")
                    tuichu()
        # 检验电话号码正确姓
        # 计算输入次数
        t = 3
        while t >= 0:
            try:
                telphone = input("请输入要修改的联系电话:")
                if telphone.isdigit():
                    if len(telphone) == 11:
                        # 将输入的信息加入字典中
                        dict[name] = {"性别": sex, "电话": telphone}
                        print("添加中")
                        sj()
                        print("\n添加成功")
                        break
                    else:
                        if t > 0:
                            print("请输入正确电话号码! 你还有%s次输入机会" % t)
                            t -= 1
                        else:
                            print("达到最大输入次数,系统自动退出!")
                            tuichu()
                else:
                    print("请输入数字!")
            except Exception:
                print("请输入正确数字")
    except Exception:
        print("未找到此人,请重新输入")
    else:
        print("修改中")
        sj()
        print("\n修改成功")
        dict[name] = {"性别": sex, "电话": telphone}

def chaxun():
    try:
        name = input("请输入你要查询的姓名:")
        print("查询中")
        sj()
        print("\n查询成功")
        print(dict[name])
    except Exception:
        print("未找到此人,请重新输入")

def huoqu():
    print("获取中")
    sj()
    print("\n获取成功")
    print("当前的名片有:")
    for key, value in dict.items():
        print(key, value)

def baocun():
    f = open("学生信息.txt", "a", encoding="utf-8")
    f.write(str(dict))
    f.close()
    print("保存中")
    sj()
    print("\n您的信息已保存到 '学生信息.txt' 中")

def jh():
    try:
        print("清理中")
        os.remove("学生信息.txt")
        sj()
        print("\n'学生信息.txt'已被删除")
    except Exception:
        print("请先创建文件!")

def xieru():
    global dict
    f = open("guanli.data", "a")
    content = f.read()
    dict = eval(content)
    f.close()

def shan():
    print("删除中")
    sj()
    dict.clear()
    print("\n删除成功")

def main():
    xieru()

while True:
    # 打印输出菜单
    xitong()
    # 进行选择
    num = shuru()
    if num == 1 or num == 2 or num == 3 or num == 4 or num == 5 or num == 6 or num == 7 or num == 8 or num == 9:
        if num == 1:
            addname()
        elif num == 2:
            deldict()
        elif num == 3:
            xiugai()
        elif num == 4:
            chaxun()
        elif num == 5:
            huoqu()
        elif num == 6:
            pass
        elif num == 7:
            baocun()
        elif num == 8:
            jh()
        elif num == 9:
            tuichu()
        else:
            print("请输入正确数字!!!")
    else:
        print("请输入正确数字!!!")

if __name__ == '__main__':
    main()

 

 

 

Logo

快速构建 Web 应用程序

更多推荐