2018-09-01-day10作業(yè)-學(xué)生信息管理系統(tǒng)

1.主界面模塊

"""__author__=wuliang"""
from time import sleep
import logout
import login
import student
import file_operation
'''
主界面模塊舟山,也是被程序的入口
'''
def start_inter():
    '''
    系統(tǒng)初始化的主界面
    :return:
    '''
    print('='*30)
    print('+',end='')
    print(' '*7,end='')
    print('學(xué)生信息管理系統(tǒng)',end='')
    print(' '*5,end='')
    print('+')

    print('+', end='')
    print(' '*10,end='')
    print('1.登錄',end='')
    print(' '*10,end='')
    print('+')

    print('+', end='')
    print(' ' * 10, end='')
    print('2.注冊', end='')
    print(' ' * 10, end='')
    print('+')

    print('+', end='')
    print(' ' * 10, end='')
    print('3.退出', end='')
    print(' ' * 10, end='')
    print('+')
    print("="*30)

    operation=input('請輸入你的選擇:')
    fun_operation(operation)

def main_operation(username):
    '''
    系統(tǒng)操作主界面
    :return:
    '''
    operation=0 #設(shè)定operation初始值0没龙,什么也不做
    while operation!=6:
        print('=' * 30)
        print('+', end='')
        print(' ' * 7, end='')
        print('學(xué)生信息管理系統(tǒng)', end='')
        print(' ' * 8, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('1.查看學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('2.添加學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('3.修改學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('4.刪除學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('5.查找學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('6.退出', end='')
        print(' ' * 13, end='')
        print('+')
        print("=" * 30)

        operation = input('請輸入你的操作選擇:')
        try:
            if isinstance(int(operation),int):
                operation = int(operation)
                while operation not in range(1,7):
                    operation = input('你輸入的操作不存在编丘,請重新輸入:')
                    if isinstance(operation, int):
                        operation = int(operation)

                operation_select(operation,username)

                sleep(2)
            else:
                print('你輸入的操作必須是(1~6)!!!')
                sleep(2)
        except:
            print('程序異常!!!')
            sleep(2)
        print('返回上一級目錄...')

def operation_select(operation:int,username:str):
    '''

    進(jìn)入操作界面后具體的操作選擇
    :param operation: 操作選擇
    :return:
    '''
    if operation==1:
        student.show_appoint_stus(username)
        sleep(2)
    elif operation==2:
        one_student=student.add_student(username)
        flag=file_operation.append_student(file_operation.get_all_students(),username,one_student)
        print(flag)
        sleep(2)
    elif operation == 3:
        stuname=input('請輸入你要修改的學(xué)生的姓名:')
        print(student.modify_student(stuname,username))
        sleep(2)

    elif operation == 4:
        stuname=input('請輸入你要刪除的學(xué)生的姓名:')
        print(student.del_student(stuname,username))
        sleep(2)
    elif operation == 5:
        stuname=input('請輸入你要查找的學(xué)生的姓名:')
        student.find_student(stuname,username)
        sleep(2)


def fun_operation(operation):
    '''
    操作方式選擇
    :param operation:
    :return:
    '''
    operation = int(operation)
    if operation == 1:
        #進(jìn)入登錄界面
        username=login.fun_login()
        if username:
            main_operation(username)
    elif operation == 2:
        #進(jìn)入注冊界面
        logout.fun_regis()
    elif operation == 3:
        #退出登錄确虱、結(jié)束本程序
        print('謝謝使用!!!')
        exit(0)


if __name__=='__main__':
    while True:
       start_inter()


2.注冊模塊

"""__author__=wuliang"""
import file_operation
teacher={}
username=''
def fun_regis():
    '''
    注冊功能實(shí)現(xiàn)
    :return:
    '''

    while True:
        username = input('請輸入你的名字:')
        if not file_operation.check_teacher(file_operation.get_all_teacher(),username):
            while len(username)<3 or len(username)>16:
                username = input('請重新輸入你的名字(3~16位):')
            print('該用戶名可以使用!!!')
            password = input('請輸入你的密碼:')
            pw = input('確認(rèn)密碼:')
            while pw != password:
                print('密碼錯誤累提,請重新輸入V宓狻财剖!')
                password = input('請輸入你的密碼:')
                pw = input('確認(rèn)密碼:')
            teacher['teaname'] = username
            teacher['password'] = pw
            #將數(shù)據(jù)存入文件中
            user_list = file_operation.get_all_teacher()
            print(file_operation.append_teacher(user_list,teacher))
            return
        print('用戶名已注冊!!!')
# def pass_input():
#     '''
#     進(jìn)行密碼確認(rèn)
#     :return:
#     '''
#     pw = ' '
#     password='  '
#
#     while pw==password:
#         password = input('請輸入你的密碼:')
#         pw = input('確認(rèn)密碼:')
#     teacher['teaname'] = username
#     teacher['password'] = pw

3.登錄模塊

"""__author__=wuliang"""
import file_operation
from time import sleep
def fun_login():
    '''

    教師登錄功能的實(shí)現(xiàn)
    :return: username
    '''
    username = input('請輸入你的姓名:')
    password = input('請輸入你的密碼:')
    user_list = file_operation.get_all_teacher()
    while True:
        try:
            if user_list.index({'teaname':username,'password':password})>=0:
                print('登錄成功幌羞,現(xiàn)在進(jìn)入操作界面...')
                sleep(1)
                return username
        except:
            print('用戶名或密碼錯誤趣避,請重新輸入')
            username = input('請輸入你的姓名:')
            password = input('請輸入你的密碼:')

4.文件操作模塊

"""__author__=wuliang"""
import os
'''
文件操作模塊,進(jìn)行文件內(nèi)容的查詢新翎,以及文件內(nèi)容的更新
'''
try:
    import cPickle as pickle
except ImportError:
    import pickle

filename = 'user.txt'

student_file='student.txt'


def mkuserfile(filename):
    '''
    如果不存在建存儲教師信息的文件并返回教師文件對象程帕,否則返回教師文件對象
    :return:文件對象
    '''
    fb = open(filename,'wb')
    return fb

def get_onlyread(filename):
    if not os.path.exists(filename):
        mkuserfile(filename).close()
    fb2 = open(filename, 'rb')
    return fb2

def check_teacher(user_list,username):
    '''
    檢查該姓名是否存在
    :param username:
    :return:
    '''

    file = get_onlyread(filename)
    if os.path.getsize(filename):
        for teacher in user_list:
            if teacher['teaname']==username:
                file.close()
                return True

    else:
        file.close()
        return False
    file.close()
    return False


def get_all_teacher():
    '''
    獲取所有的教師
    :return:
    '''
    file = get_onlyread(filename)
    if os.path.getsize(filename):
        user_list = pickle.load(file)
        file.close()
        return user_list
    else:
        file.close()
        return []



def append_teacher(teacher_list:list,teacher:dict):
    '''
    添加教師
    :param teacher_list:教師列表
    :param teacher:新教師
    :return:是否成功
    '''
    try:
        file = mkuserfile(filename)
        teacher_list.append(teacher)
        pickle.dump(list(teacher_list),file)
        file.close()
        return '保存成功'
    except:
        return '保存失敗'


def get_all_students():
    '''
    獲取所有學(xué)生
    :param username:
    :return:
    '''
    #1.根據(jù)參數(shù)獲取指定文件的內(nèi)容
    file = get_onlyread(student_file)
    #2.建立學(xué)生總列表
    stu_list = []
    #3.判斷文件是否為空
    if os.path.getsize(student_file):

        stu_list=pickle.load(file)

    #4.關(guān)閉文件
    file.close()
    return stu_list

def append_student(student_list:list,username:str,student:dict):
    '''
    添加指定教師的學(xué)生
    :param student_list:學(xué)生的總列表
    :param username:教師姓名
    :param student:新學(xué)生
    :return:添加成功 or 失敗
    '''
    if student_list==[]:
        file = mkuserfile(student_file)
        pickle.dump([[username,student]],file)
        file.close()
        return '添加成功'
    for one_stu_list in student_list:
        if one_stu_list[0]==username:
            one_stu_list.append(student)
            student_list[student_list.index(one_stu_list)]=one_stu_list
            file = mkuserfile(student_file)
            pickle.dump(student_list,file)
            file.close()
            return "添加成功"

    file = mkuserfile(student_file)
    student_list.append([username, student])
    pickle.dump((student_list), file)
    file.close()
    return '添加成功'

def get_appoint_stu(username):
    '''
    獲取指定教師的學(xué)生
    :param username: 教師姓名
    :return: stu_appoint_list

    '''
    stu_appoint_list=[]
    all_list = get_all_students()
    for s_l in all_list:
        if s_l[0]==username:
            stu_appoint_list=s_l
            break
    return stu_appoint_list

def check_student(username:str,stuname:str):
    '''
    檢查是否該教師下是否已經(jīng)有了那個學(xué)生
    :param stuname: 學(xué)生姓名
    :param username: 教師姓名
    :return:False:名字已存在,True:可以注冊
    '''
    stu_list=get_appoint_stu(username)
    if stu_list:
        for stu in stu_list[1:]:
            if stu['stuname']==stuname:
                return False
    return True

def modify_student(student_list:list):
    '''
    刪除學(xué)生并更新表
    :param student_list: 已經(jīng)刪除了學(xué)生的的表
    :return:True or False
    '''
    try:
        file = mkuserfile(student_file)
        pickle.dump(student_list, file)
        file.close()
        return True
    except:
        return False

def get_onestudnet(stuname:str,username:str):
    '''
    獲取指定學(xué)生姓名的學(xué)生
    :param stuname:
    :param username:
    :return:
    '''
    appoint_stu_list=get_appoint_stu(username)
    for stu in appoint_stu_list[1:]:
        if stu['stuname']==stuname:
            return stu
    return None

5.學(xué)生模塊

"""__author__=wuliang"""
import file_operation

def add_student(username):
    '''
    添加學(xué)生地啰,獲取學(xué)生的輸入
    :return:
    '''
    stuname = input('請輸入你要添加的學(xué)生的姓名:')
    while not file_operation.check_student(username,stuname):
        print('對不起輸入的學(xué)生的姓名存在!!!')
        stuname = input('請重新輸入學(xué)生的姓名:')
    print('該姓名可以注冊!!!')
    stuid = input('請輸入你要添加的學(xué)號:')
    phone_num = input('請輸入你要添加的電話:')
    age = input('請輸入你要添加的年齡')
    student={'stuname':stuname,'stuid':stuid,'phone_num':phone_num,'age':age}
    return student


def show_appoint_stus(username:str):
    '''
    查看當(dāng)前教師所有學(xué)生信息
    :param username:
    :return:
    '''
    stu_list=file_operation.get_appoint_stu(username)
    for student_ in stu_list[1:]:
        print('stuname:'+student_['stuname']+' stuid:'+student_['stuid'],end=' ')
        print('phone_num:' + student_['phone_num'] + ' age:' + student_['age'])

def modify_student(stuname:str,username:str):
    '''

    :param stuname:
    :param username:
    :return:
    '''
    stu_appoint_list = file_operation.get_appoint_stu(username)
    for stu in stu_appoint_list[1:]:
        if stu['stuname']==stuname:
            print('你要修改的學(xué)生存在!!')
            new_stuname=input('請輸入新的姓名:')
            while not file_operation.check_student(username,new_stuname):
                print('你輸入的名字已存在!!!')
                new_stuname=input('請重新輸入姓名:')
            new_stuid = input('請輸入新的學(xué)號:')
            new_phone_num = input('請輸入新的電話號碼:')
            new_age = input('請輸入新的年齡:')
            student_={'stuname':new_stuname,'stuid':new_stuid,'phone_num':new_phone_num,'age':new_age}
            all_stu_list=file_operation.get_all_students()
            all_stu_list.remove(stu_appoint_list)

            stu_appoint_list.remove(stu)
            stu_appoint_list.append(student_)

            all_stu_list.append(stu_appoint_list)

            file_operation.modify_student(all_stu_list)
            return '修改成功'

def del_student(stuname:str,username:str):
    '''

    :param stuname:
    :param username:
    :return:
    '''
    stu_all_list=file_operation.get_all_students()

    stu_appoint_list=file_operation.get_appoint_stu(username)
    stu_all_list.remove(stu_appoint_list)
    for stu in stu_appoint_list[1:]:
        if stu['stuname']==stuname:
            stu_appoint_list.remove(stu)
            stu_all_list.append(stu_appoint_list)
            file_operation.dmodify_student(stu_all_list)
            return '刪除成功'
            break
    print('你要刪除的學(xué)生不存在')
    return '刪除失敗!!'

def find_student(stuname:str,username:str):
    flag=file_operation.get_onestudnet(stuname,username)
    if flag:
        print('stuname:'+flag['stuname']+' stuid:'+flag['stuid'],end=' ')
        print('stuname:'+flag['phone_num']+' age:'+flag['age'])
    else:
        print('對不起愁拭,你輸入的學(xué)生不存在')

6.文件設(shè)計(jì)

user.txt:存儲教師信息文件
存儲格式:
  [{'teaname':教師名,'password':登錄密碼}]

student.txt:存儲學(xué)生信息文件
存儲格式:
  [[教師名,{'stuname':學(xué)生名,'stuid':學(xué)號,'phone_num':學(xué)生電話號碼,'age':年齡},...],...]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市亏吝,隨后出現(xiàn)的幾起案子岭埠,更是在濱河造成了極大的恐慌,老刑警劉巖蔚鸥,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惜论,死亡現(xiàn)場離奇詭異,居然都是意外死亡止喷,警方通過查閱死者的電腦和手機(jī)馆类,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來弹谁,“玉大人乾巧,你說我怎么就攤上這事句喜。” “怎么了沟于?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵咳胃,是天一觀的道長。 經(jīng)常有香客問我旷太,道長展懈,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任供璧,我火速辦了婚禮存崖,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嗜傅。我一直安慰自己金句,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布吕嘀。 她就那樣靜靜地躺著违寞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪偶房。 梳的紋絲不亂的頭發(fā)上趁曼,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音棕洋,去河邊找鬼挡闰。 笑死,一個胖子當(dāng)著我的面吹牛掰盘,可吹牛的內(nèi)容都是我干的摄悯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼愧捕,長吁一口氣:“原來是場噩夢啊……” “哼奢驯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起次绘,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤瘪阁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后邮偎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體管跺,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年禾进,在試婚紗的時候發(fā)現(xiàn)自己被綠了豁跑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡命迈,死狀恐怖贩绕,靈堂內(nèi)的尸體忽然破棺而出火的,到底是詐尸還是另有隱情壶愤,我是刑警寧澤淑倾,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站征椒,受9級特大地震影響娇哆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勃救,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一碍讨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蒙秒,春花似錦勃黍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瓢省,卻和暖如春弄息,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勤婚。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工摹量, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人馒胆。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓缨称,卻偏偏與公主長得像,于是被迫代替她去往敵國和親祝迂。 傳聞我的和親對象是個殘疾皇子睦尽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,304評論 25 707
  • 非暴力溝通的第二要素是感受。體會和表達(dá)感受是件很重要的事情液兽。在生活中骂删,感受往往與看法相近,體會和表達(dá)感受并...
    紅芝_d3fa閱讀 8,080評論 0 0
  • 一個語言表達(dá)不是很好的人四啰,怎么才能把想表達(dá)的說出來呢宁玫,每次想說出口的時候,停住了柑晒,猶豫了欧瘪,也不知道為什么,也許自己...
    那一夏舊時光閱讀 130評論 0 0
  • 1匙赞、出名要趁早佛掖,還是大器晚成 遙想公瑾當(dāng)年妖碉,小喬初嫁了,雄姿英發(fā)芥被,羽扇綸巾欧宜。自古以來夭坪,國人都偏愛少年英雄戈二。這也難怪...
    江左不歸人閱讀 338評論 0 1