2019-07-30 學(xué)生信息管理系統(tǒng)

"""__author__ = fzzy"""
""" Create Time:  2019/8/6  17:35 """


import random
import datetime
import string

visitor = ''               # 登錄賬號(hào),用于功能界面顯示
id_info = {}               # 賬號(hào)字典
stu_info = {}              # 學(xué)生字典
idpath = 'C:\\Users\\13155\Desktop\\id.txt'    # 賬號(hào)信息存儲(chǔ)文件路徑
studentpath = ''         # 學(xué)生信息存儲(chǔ)文件路徑

# 將字典存入txt
def save_dict_to_file(dict, filepath):
    try:
        with open(filepath,'a') as  f:   # a:附加寫方式打開,不可讀
            for key in dict:
                f.write('%s:%s\n'%(key,dict[key]))
    except IOError as ioerr:
        print('文件%s無法創(chuàng)建'%filepath)

# 從txt中加載字典
def load_dict_from_file(dict,filepath):
    try:
        with open(filepath,'r') as f:
            for line in f:
                key, value = line.strip().split(':')
                dict[key] = value
    except IOError as ioerr:
        print('文件%s不存在' % filepath)


# 添加學(xué)生
def add_student():
    randomNum = random.randint(0, 100)      #學(xué)號(hào)等于當(dāng)前日期轉(zhuǎn)格式后加1~100的隨機(jī)數(shù)的字符串
    nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    stu_number = str(nowTime) + str(randomNum)
    name = input('\n請(qǐng)輸入姓名:')            #輸入
    stu_id = int(stu_number)
    age = int(input('請(qǐng)輸入年齡:'))
    if age < 0:                              #輸入檢測
        print('\n輸入錯(cuò)誤!')
        age = int(input('請(qǐng)重新輸入年齡:'))
        print('')
    sex = input('請(qǐng)輸入性別:')
    while sex != '男' and sex != '女':
        print('\n輸入錯(cuò)誤!')
        sex = input('請(qǐng)重新輸入性別:')
        print('')
    stu_class = input('請(qǐng)輸入班級(jí):')
    stu_info.clear()          #  清空列表,否則存入時(shí)會(huì)重復(fù)存入之前的值
    stu_info[name] = [stu_id, age, sex, stu_class]
    save_dict_to_file(stu_info, studentpath)
    load_dict_from_file(stu_info,studentpath)
    if name in stu_info:         # 檢測是否成功添加
        print('\n添加成功!')
    else:
        print('\n添加失敗!')
    print('\n1.繼續(xù)添加')
    print('2.返回')
    selet = int(input('\n請(qǐng)選擇:'))
    if selet == 1:
        add_student()
    elif selet == 2:
        function()
    else:
        print('\n輸入錯(cuò)誤!')
        selet = int(input('請(qǐng)重新選擇:'))


# 查看學(xué)生
def check_student():
    load_dict_from_file(stu_info,studentpath)
    print('=================學(xué)生列表======================')
    for dict in stu_info:
        str1  = stu_info[dict].replace('[','').replace(']','')  # 由于字典的值本來是列表,但存入是以字符串形式
        list1 = str1.split(',')  #以,為間隔符字符串轉(zhuǎn)列表        #存入的,導(dǎo)出時(shí)為了分開,需要把[ 和 ]去掉
        print('姓名:',dict)
        print('學(xué)號(hào):',eval(list1[0]))
        print('年齡:',eval(list1[1]))
        print('性別:',eval(list1[2]))
        print('班級(jí):',eval(list1[3]))
        print('')

    print('=============================================')
    print('1.返回')
    selet = int(input('\n請(qǐng)選擇:'))
    if selet == 1:
        function()
    else:
        print('\n輸入錯(cuò)誤!')
        selet = int(input('請(qǐng)重新選擇:'))

# 修改學(xué)生信息
def updata_student():
    load_dict_from_file(stu_info,studentpath)
    name = input('\n請(qǐng)輸入要更新信息的學(xué)生姓名:')      #修改信息
    if name not in stu_info:
        print('該學(xué)生不存在,請(qǐng)重新確認(rèn)!\n')
        updata_student()
    else:
        stu_id = int(input('請(qǐng)輸入學(xué)號(hào):'))
        age =int(input('請(qǐng)輸入年齡:'))
        if age < 0:
            print('\n輸入錯(cuò)誤!')
            age = int(input('請(qǐng)重新輸入年齡:'))
            print('')
        sex = input('請(qǐng)輸入性別:')
        while sex != '男' and sex != '女':
            print('\n輸入錯(cuò)誤!')
            sex = input('請(qǐng)重新輸入性別:')
            print('')
        stu_class = input('請(qǐng)輸入班級(jí):')
        stu_info[name] = [stu_id ,age, sex, stu_class]
        f = open(studentpath,'r+')       # r+讀寫,不創(chuàng)建
        f.truncate()                     # 清空txt文件,否則會(huì)出現(xiàn)重復(fù)數(shù)據(jù)
        save_dict_to_file(stu_info,studentpath)
        load_dict_from_file(stu_info,studentpath)
        print(stu_info[name])
        if stu_info[name] == str([stu_id, age, sex, stu_class]):  # 檢測是否修改成功
            print('\n修改成功!')
        else:
            print('\n修改失敗!')
        print('\n1.繼續(xù)修改')
        print('2.返回')
        selet = int(input('\n請(qǐng)選擇:'))
        if selet == 1:
            updata_student()
        elif selet == 2:
            function()
        else:
            print('\n輸入錯(cuò)誤!')
            selet = int(input('請(qǐng)重新選擇:'))

# 刪除學(xué)生
def del_student():
    load_dict_from_file(stu_info,studentpath)
    name = input('\n請(qǐng)輸入要?jiǎng)h除的學(xué)生姓名:')
    if name not in stu_info:
        print('該學(xué)生不存在,請(qǐng)重新確認(rèn)!\n')
        del_student()
    else:
        stu_info.pop(name)
        f = open(studentpath,'r+')
        f.truncate()
        save_dict_to_file(stu_info,studentpath)
        load_dict_from_file(stu_info,studentpath)
        if name not in stu_info:       # 檢測是否刪除成功
            print('\n刪除成功!')
        else:
            print('\n刪除失敗!')
        print('\n1.繼續(xù)刪除')
        print('2.返回')
        selet = int(input('\n請(qǐng)選擇:'))
        if selet == 1:
            del_student()
        elif selet == 2:
            function()
        else:
            print('\n輸入錯(cuò)誤!')
            selet = int(input('請(qǐng)重新選擇:'))

# 返回注冊登錄界面
def return_main():
    print('\n\n')
    main_window()

# 功能界面
def function():
    print('\n================================')
    print('??????歡迎%s??????\n'%visitor)
    print('◆1.添加學(xué)生')
    print('◆2.查看學(xué)生')
    print('◆3.修改學(xué)生信息')
    print('◆4.刪除學(xué)生')
    print('◆5.返回')
    choice = int(input('\n請(qǐng)選擇:'))         # 根據(jù)輸入選擇功能
    if choice == 1:
        add_student()
    elif choice == 2:
        check_student()
    elif choice == 3:
        updata_student()
    elif choice == 4:
        del_student()
    elif choice == 5:
        return_main()


# 注冊界面
def res_window():
    print('')
    user_id = input('請(qǐng)輸入您要?jiǎng)?chuàng)建的賬號(hào):')
    while len(user_id) < 3 or len(user_id) > 6:
        print('\n您應(yīng)該創(chuàng)建一個(gè)3~6位的賬號(hào)!')
        user_id = input('請(qǐng)輸入您要?jiǎng)?chuàng)建的賬號(hào):')
    load_dict_from_file(id_info,idpath)
    if user_id in id_info:                         # 檢測賬號(hào)是否存在
        print('該賬號(hào)已經(jīng)存在,請(qǐng)重新輸入!')
        res_window()
    else:
        password = input('請(qǐng)輸入您的密碼:')
        if len(password) < 6:                    # 檢測密碼長度是否大于6位
            print('\n密碼長度最少為6位!')
            password = input('請(qǐng)重新設(shè)計(jì)您的密碼:')
        confirm_password = input('確認(rèn)您的密碼:')
        while password != confirm_password:
            print('\n兩次輸入的密碼不一致,請(qǐng)重新輸入!\n')     # 檢測兩次輸入的密碼是否一樣
            password = input('請(qǐng)輸入id為%s的密碼:'%user_id)
            if len(password) < 6:
                print('\n密碼長度最少為6位!')
                password = input('請(qǐng)重新設(shè)計(jì)您的密碼:')
            confirm_password = input('確認(rèn)您的密碼:')
        id_info.clear()
        id_info[user_id] = password
        save_dict_to_file(id_info, idpath)
        load_dict_from_file(id_info,idpath)
        if user_id in id_info:               #檢測是否創(chuàng)建成功
            print('\n創(chuàng)建成功!')
        else:
            print('\n創(chuàng)建失敗!')
        main_window()

# 登錄界面
def sign_window():
    load_dict_from_file(id_info,idpath)
    user_id1 = input('\n請(qǐng)輸入賬號(hào):')
    while user_id1 not in id_info:
        print('您輸入的賬號(hào)不存在!\n')
        user_id1 = input('請(qǐng)重新輸入賬號(hào):')
    password1 = input('請(qǐng)輸入密碼:')
    while password1 != id_info[user_id1]:
        print('您輸入的密碼不正確!\n')
        password1 = input('請(qǐng)重新輸入密碼:')
    print('登錄成功!')
    global visitor
    visitor = user_id1           # 將登陸賬號(hào)賦給變量,在功能界面輸出
    global studentpath
    studentpath =  'C:\\Users\\13155\Desktop\\record\\'+ visitor +'.txt'
    function()

# 主界面
def main_window():
    print( '=============學(xué)生信息管理系統(tǒng)=============')
    print('◆ 1.注冊')
    print('◆ 2.登錄')
    choice = int(input('\n請(qǐng)選擇:(1 - 2):'))
    if choice == 1:
        res_window()
    elif choice == 2:
        sign_window()
    else:
        print('\n輸入錯(cuò)誤,請(qǐng)重新輸入!')
        print('=====================================')
        choice = choice = int(input('請(qǐng)選擇:(1 - 2):'))

# 開始
main_window()




最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容