1. 程式人生 > >python-學生資訊管理系統

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("帳號無效")