python-学生信息管理系统

import json
import time


studentinfo = """
            1.查询个人信息
            2.修改信息
            3.退出登陆

"""

update_student="""
            1.修改名字
            2.修改年龄
            3.修改密码

"""
rootface =  """

        1.管理员密码修改
        2.添加学生信息
        3.删除学生信息
        4.修改学生信息
        5.查询学生信息(按学号,按姓名)
        6.查看所有学生信息
        7.退出系统
        
"""

def check(card_id,dict_info):   #检查帐号是否有效
    if card_id in dict_info:
        return True
    else:
        return False

def add_info(dict_info):   ##添加用户
    print("带*的选填")
    card_id = input("请输入要添加的学号:")
    if check(card_id,dict_info):
        print("用户已经存在")
    else:
        li=[]
        try:
            name = input("输入姓名:")
            li.append(name)
        except ValueError:
            print("不能为空")
        else:
            print("添加姓名成功")
        try:
            sex=int(input("性别(男1,女2):"))
            li.append(sex)
        except ValueError:
            print("性别不为空")
        else:
            print("性别添加成功")
            age=input("*年龄:")
            if not age:
                age="保密"
            li.append(age)
            grade=input("*班级:")
            if not grade:
                grade="保密"
            li.append(grade)
            birthday=input("*出生年月:")
            if not birthday:
                birthday="保密"
            li.append(birthday)
            stu_passwd=input("登陆密码:")
            if not  stu_passwd:
                stu_passwd='111111'
            li.append(stu_passwd)
            print("密码添加成功")
            li.append(card_id)
        if len(li) == 7:
            return card_id,li
        return 0,0

def update_passwd(root_info):  #修改管理员密码
    passwd=input("请输入旧密码:")
    if passwd == root_info.get('000'):
        new_passwd=input("请输入新密码:")
        root_info['000']=new_passwd
        print("密码修改成功")
    else:
        print("密码错误")

def delete_info(dict_info):   #删除学生信息
    card_id=input("请输入你要删除的学号:")
    if check(card_id,dict_info):
        dict_info.pop(card_id)
        print("成功删除%s" %card_id)
    else:
        print("该学号不存在")

def update_stuinfo(dict_info,card_id):  #修改学生信息
        if check(card_id,dict_info):
            value_li=dict_info.get(card_id)
            update=input("你要修改什么信息(name,sex,age,grade,birthday,passwd):")
            if update == 'name':
                new_name=input("修改姓名:")
                value_li[0]=new_name
                print("姓名修改成功")
            elif update == 'sex':
                new_sex=int(input("修改性别:"))
                value_li[1]=new_sex
                print("修别修改成功")
            elif update=='grade':
                new_grade=input("修改班级:")
                value_li[3]=new_grade
                print("班级修改成功")
            elif update=='birthday':
                new_birthday=input("修改出生年月:")
                value_li[4]=new_birthday
                print("出生年月修改成功")
            elif update=='age':
                new_age=input("修改年龄:")
                value_li[2]=new_age
                print("年龄修改成功")
            elif update == 'passwd':
                new_passwd=input("修改密码:")
                value_li[5]=new_passwd
                print("密码修改成功")
            else:
                print("请输入正确的信息")

        else:
            print("该学号不存在")


def search_info(dict_info):  #查询学生信息
    chioce=int(input("按学号查询按1,按姓名查询按2:"))
    if chioce == 1:
        card_id = input("输入你要查询学生的学号:")
        if check(card_id,dict_info):
            print(dict_info[card_id])
        else:
            print("未搜索到信息")
    elif chioce ==2:
        name=input("输入你要查询学生的姓名:")
        name_li=[]
        li=[]
        for info in dict_info.values():
            name_li.append(info[0])
        if name in name_li:
            for info in dict_info.values():
                li.append(info)
            else:
                print(li[name_li.index(name)])
        else:
            print("未搜索到该学生")

def addfile(x):   #写入学生信息到文件
    filename = 'manage'
    with open(filename,'w') as f_atm:
        json.dump(x,f_atm)

def addfile1(x):
    filename = 'rootinfo'
    with open(filename,'w') as f_atm:
        json.dump(x,f_atm)

def readfile(x):  #读取文件
    filename =x
    with open(filename) as f_atm:
        x = json.load(f_atm)
    return x
def time_face():
    print(time.ctime())

dict_info = readfile('./manage')
root_info = readfile('./rootinfo.txt')

while True:
    print ("登陆界面".center(20,'*'))
    time_face()
    card_id = input("请输入你的帐号(学号):")
    if card_id == '000':    #管理员登陆
        passwd = input("请输入密码:")
        if passwd == 'redhat':
            while True:
                print("管理员登陆".center(10, '*'))
                time_face()
                print(rootface)
                choice = int(input("请输入你要的功能:"))
                if choice == 1:
                    update_passwd(root_info)
                    addfile1(root_info)
                elif choice == 2:
                    keys, values = add_info(dict_info)
                    if keys !=0:
                        dict_info[keys] = values
                        addfile(dict_info)
                    else:
                        print("信息不全,添加失败")
                elif choice == 3:
                    delete_info(dict_info)
                    addfile(dict_info)
                elif choice == 4:
                    card_id = input("输入你要修改学生的学号:")
                    update_stuinfo(dict_info, card_id)
                    addfile(dict_info)
                elif choice == 5:
                    search_info(dict_info)
                elif choice == 6:
                    for values in dict_info.values():
                        print(values)
                elif choice == 7:
                    time_face()
                    print("退出系统")
                    break
        else:
            print("密码错误")
    elif check(card_id,dict_info):
        passwd = input("请输入你的密码:")
        if passwd == dict_info.get(card_id)[-2]:
            while True:
                print("学生登陆".center(10, '*'))
                time_face()
                print(studentinfo)
                choice = int(input("输入你要的功能:"))
                if choice == 1:
                    print(dict_info.get(card_id))
                elif choice == 2:
                    value_li = dict_info.get(card_id)
                    print(update_student)
                    update = input("你要修改什么信息(name,age,passwd):")
                    if update == '1':
                        new_name = input("修改姓名:")
                        value_li[0] = new_name
                        addfile(dict_info)
                        print("姓名修改成功")
                    elif update == '2':
                        new_age = input("修改年龄:")
                        value_li[2] = new_age
                        addfile(dict_info)
                        print("年龄修改成功")
                    elif update == '3':
                        new_passwd = input("修改密码:")
                        value_li[5] = new_passwd
                        addfile(dict_info)
                        print("密码修改成功")
                    else:
                        print("请输入正确的信息")
                elif choice == 3:
                    print("退出学生登录")
                    time_face()
                    break
        else:
            print("密码错误")
    else:
        print("帐号无效")

你可能感兴趣的:(Python语言)