最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于python制作簡易版學(xué)生信息管理系統(tǒng)

 更新時間:2021年04月19日 17:04:43   作者:Falcont  
這篇文章主要介紹了基于python制作簡易版學(xué)生信息管理系統(tǒng),文中有非常詳細的代碼示例,對正在學(xué)習python的小伙伴們有很好地幫助,需要的朋友可以參考下

一、前言

本篇博客對于文件操作、字典、列表、匿名函數(shù)以及sort()等內(nèi)置函數(shù)進行了系統(tǒng)的整理操作,以設(shè)計一個學(xué)生信息管理系統(tǒng)的形式展示,具體概念方法等會在代碼后進行分析講述,請讀者仔細分析每一處解析,對于基礎(chǔ)鞏固將會有很大的幫助,其中還有每一塊代碼的設(shè)計思路圖,邏輯分析會有一定的提升。

二、需求分析

在這里插入圖片描述

在這里插入圖片描述

本程序需要用到os模板首先導(dǎo)入,并命名要存儲信息的文件

import os
File_Object_Name = 'Student_Inforation.txt'

三、主函數(shù)

在這里插入圖片描述

def Main():
    while True:
        Menu()
        _Select = int(input('Please select operation:'))
        if _Select in [0, 1, 2, 3, 4, 5, 6, 7]:
            if _Select == 0:
                _Answer = input('Do you want to log out?(Y/N)')
                if _Answer == 'Y' or _Answer == 'y':
                    print('Thank for you use!!!')
                    break
                    pass
                else:
                    continue
                    pass
                pass
            elif _Select == 1:
                Insert_Infor()
                pass
            elif _Select == 2:
                Search_Infor()
                pass
            elif _Select == 3:
                Delete_Infor()
                pass
            elif _Select == 4:
                Change_Infor()
                pass
            elif _Select == 5:
                Sort()
                pass
            elif _Select == 6:
                Count_Total_Num()
                pass
            elif _Select == 7:
                Show_Infor()
                pass
            pass
        else:
            print('Error Input!!!')
            pass

四、功能菜單

def Menu():
    print('=========Student Information Management System=========')
    print('---------------------Function menu---------------------')
    print('             1、Input Student Information')
    print('             2、Search Student Information')
    print('             3、Delete Student Information')
    print('             4、Change Student Information')
    print('             5、Sort According to Score')
    print('             6、Count Total Student Number')
    print('             7、Show All Student Information')
    print('             0、Log Out')
    print('-------------------------------------------------------')
    pass

五、錄入信息

在這里插入圖片描述

def Insert_Infor():
    Student_Infor_List = []  # 創(chuàng)建一個學(xué)生信息空列表,后面會用到
    while True:
        Stu_Id = input('Please input the id(such as 1001,1002):')
        if not Stu_Id:  # 空的字符串轉(zhuǎn)為bool類型為False,非空為True,此處若是輸入為空會直接跳出循環(huán)。(空列表、字典、元組等都滿足)
            break
            pass
        Stu_Name = input('Please input the name:')
        if not Stu_Name:
            break
            pass
        try:
            English_Score = int(input('Please input the English score:'))
            Python_Score = int(input('Please input the Python score:'))
            Java_Score = int(input('Please input the Java score:'))
            pass
        except:
            print('Error Input(not int),please input again')
            continue
            pass
        # 將每個學(xué)生的信息放入一個字典當中
        Student_Infor_Dict = {'Id': Stu_Id, 'Name': Stu_Name, 'English': English_Score, 'Python': Python_Score, 'Java': Java_Score}
        # 將一個字典作為一個元素放入學(xué)生信息列表
        Student_Infor_List.append(Student_Infor_Dict)  # 使用 append() 方法來添加列表項
        _Answer = input('Whether to input more?(Y/N)')
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            break
            pass
        pass
    # 將學(xué)生信息列表中的字典元素保存到文件之中,調(diào)用Keep_Infor()函數(shù)
    Keep_Infor(Student_Infor_List)
    print('Input Finished')
    pass

六、保存信息

def Keep_Infor(List):
    File_Object = open(File_Object_Name, mode='a', encoding='utf-8')
    # 打開一個文件用于追加。如果該文件已存在,文件指針將會放在文件的結(jié)尾。也就是說,新的內(nèi)容將會被寫入到已有內(nèi)容之后。如果該文件不存在,創(chuàng)建新文件進行寫入。
    for item in List:
        File_Object.write(str(item)+'\n')
        pass
    # 將傳入的列表中的字典元素強制轉(zhuǎn)換成字符串類型并拼接上換行符寫入文件
    File_Object.close()

七、查找信息

在這里插入圖片描述

def Search_Infor():
    Student_Query = [] # 空列表,之后用于存放查找到的學(xué)生信息
    while True:
        Student_Id = ''
        Student_Name = ''
        if os.path.exists(File_Object_Name):
        # os.path模塊主要用于文件的屬性獲取,os.path.exists()就是判斷括號里的文件是否存在,括號內(nèi)的可以是文件路徑/名。
            Search_Mode = int(input('search by id(1) or by name(2):'))
            if Search_Mode == 1:
                Student_Id = input('Please input id:')
                pass
            elif Search_Mode == 2:
                Student_Name = input('Please input name:')
                pass
            else:
                print('Error Input,try again!')
                Search_Infor()
                pass
            with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:  # 只讀模式打開文件并將類對象賦給File_Object
                Student_List = File_Object.readlines()  # readlines()按行讀取,一次性讀取所有內(nèi)容,返回一個列表,每一行內(nèi)容作為一個元素
                for item in Student_List:  # 遍歷列表中元素
                    _Dict = eval(item)
                    # 由于是以字符串類型保存到文件中,此時item相當于'dict'形式,要將其用eval()函數(shù)轉(zhuǎn)換為字典,即脫掉''。
                    if Student_Id:  # 如果選的是按照姓名查找,則此空字符串已經(jīng)不再為空,bool類型為True
                        if Student_Id == _Dict['Id']:
                            Student_Query.append(_Dict)
                            pass
                        pass
                    elif Student_Name:
                        if Student_Name == _Dict['Name']:
                            Student_Query.append(_Dict)
                            pass
                        pass
                    pass
                if Student_Query == []:  # 仍為空說明未找到。
                    print('no such student,try again')
                    Search_Infor()  # 再次調(diào)用函數(shù)
                    pass
                pass
            pass
        else:
            print('Still no such file to keep student information!')
            pass
        print(Student_Query[0])  # 此時該列表中只有一個字典元素,直接打印輸出
        Student_Query.clear()  # 調(diào)用內(nèi)置函數(shù)清空列表,方便下次使用
        _Answer = input('Whether to search others?(Y/N)')
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            return
        pass
    pass

八、刪除信息

在這里插入圖片描述

def Delete_Infor():
    while True:
        Student_Id = input('Please input the student‘s id that you want to delete:')
        if Student_Id:
            if os.path.exists(File_Object_Name):
                with open(File_Object_Name, 'r', encoding='utf-8')as File1:
                    Old_Student_Infor = File1.readlines()  # 讀取每行作為元素放入Old_Student_Infor列表
                    pass
                pass
            else:
                Old_Student_Infor = []
                pass
            Flag = False  # 是否刪除成功的標志
            if Old_Student_Infor:
                with open(File_Object_Name, 'w', encoding='utf-8')as File2:
                    _Dict = {}
                    for item in Old_Student_Infor:
                        _Dict = eval(item)  # 將刪除信息之前的列表元素轉(zhuǎn)化為字典形式賦給_Dict
                        if _Dict['Id'] != Student_Id:
                            File2.write(str(_Dict)+'\n')
                            # 如果與要刪除的信息的Id不同,覆蓋寫入原文件
                            pass
                        else:
                            Flag = True  # 如果相同的話,則不寫入文件中,相當于刪除成功
                            pass
                        pass
                    if Flag:
                        print('Student information of {} has been delete'.format(Student_Id))
                        pass
                    else:
                        print('Can not find student of id:{}'.format(Student_Id))
                        pass
                    pass
                pass
            else:
                print('file have no student information')
                break
                pass
            Show_Infor()
            _Answer = input('Whether to delete more?(Y/N)')
            pass
        if _Answer == 'Y' or _Answer == 'y':
            continue
            pass
        else:
            break
            pass
        pass
    pass

九、修改信息

在這里插入圖片描述

def Change_Infor():
    Show_Infor()
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as old_file:
            Old_Student_Infor = old_file.readlines()
            pass
        pass
    else:
        return
    Student_Id = input('Please input the id you wanna change:')
    if Student_Id:
        with open(File_Object_Name, 'w', encoding='utf-8')as new_file:
            for item in Old_Student_Infor:
                _dict = dict(eval(item))
                if _dict['Id'] == Student_Id:
                    print('Find it,can change it!')
                    while True:
                        try:
                            _dict['Name'] = input('Please input new name:')
                            _dict['English'] = input('Please input new English score:')
                            _dict['Python'] = input('Please input new Python score:')
                            _dict['Java'] = input('Please input new Java score:')
                            pass
                        except:
                            print('Error Input!!!Try again')
                            pass
                        new_file.write(str(_dict)+'\n')
                        print('Successfully change!')
                        break
                        pass
                    pass
                else:
                    print('Can‘t find it')
                    new_file.write(str(_dict)+'\n')
                    pass
                pass
            pass
        pass
    _Answer = input('Whether to change more?(Y/N)')
    if _Answer == 'y' or _Answer == 'Y':
        Change_Infor()
        pass
    else:
        return
    pass

十、顯示信息

在這里插入圖片描述

def Show_Infor():
    Infor_List = []
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Stu_List = File_Object.readlines()
            for item1 in Stu_List:
                Infor_List.append(dict(eval(item1)))
                pass
            if Infor_List:
                for item2 in Infor_List:
                    print(item2)
                    pass
                pass
            else:
                print('no student')
                pass
            pass
        pass
    else:
        print('no such file')
        pass
    pass

十一、按成績排序

在這里插入圖片描述

def Sort():
    Show_Infor()
    Infor_List = []
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Student_List = File_Object.readlines()
            for item in Student_List:
                _Dict = dict(eval(item))
                Infor_List.append(_Dict)
                pass
            pass
        pass
    else:
        print('no such file')
        return
    Sort_Mode = bool(input('Please input sort mode(0、ascending order|1、descending order)'))
    if not Sort_Mode:  # ascending order
        reverse_mode = True
        pass
    else:  # descending order
        reverse_mode = False
        pass
    Specific_Sort_Mode = int(input('Sort by English(1),by Python(2),by Jave(3),by total(4):'))
    if Specific_Sort_Mode == 1:
        Infor_List.sort(key=lambda x: int(x['English']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 2:
        Infor_List.sort(key=lambda x: int(x['Python']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 3:
        Infor_List.sort(key=lambda x: int(x['Java']), reverse=reverse_mode)
        pass
    elif Specific_Sort_Mode == 4:
        Infor_List.sort(key=lambda x: int(x['English']) + int(x['Python']) + int(x['Java']), reverse=reverse_mode)
        pass
    else:
        print('Error Input,try again')
        Sort()
        pass
    for item in Infor_List:
        print(item)
        pass
    pass
  • sort()函數(shù)原型: list.sort(key=None, reverse=False)

key參數(shù) :
key接受的是一個只有一個形參的函數(shù),形式如下
def f(a):
return len(a)
key接受的函數(shù)返回值,表示此元素的權(quán)值,sort將按照權(quán)值大小進行排序
reverse參數(shù) :
reverse接受的是一個bool類型的值 (Ture or False),表示是否顛倒排列順序,一般默認的是False

十二、統(tǒng)計人數(shù)

在這里插入圖片描述

def Count_Total_Num():
    if os.path.exists(File_Object_Name):
        with open(File_Object_Name, 'r', encoding='utf-8')as File_Object:
            Stu_List = File_Object.readlines()
            _Count = len(Stu_List)
            if _Count >= 2:
                print('A total of {} students')
                pass
            elif _Count ==1:
                print('A single student!')
                pass
            else:
                print('no student!')
                pass
            pass
        pass
    else:
        print('still no such file!')
        pass
    pass

十三、最后記得運行主函數(shù)

Main()

十四、將程序打包成可執(zhí)行exe文件

1.cmd進入command界面

2.輸入pip install PyInstaller

3.安裝完成后仍在cmd界面輸入pyinstaller -F py為擴展名的文件路徑\文件名.py

4.操作后將會有一大串代碼,倒數(shù)第二行會有最終文件的保存地址,打開之后將之前編譯程序生成的txt文件移入該文件夾中即可操作

到此這篇關(guān)于基于python制作簡易版學(xué)生信息管理系統(tǒng)的文章就介紹到這了,更多相關(guān)python學(xué)生信息管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實現(xiàn)簡單猜單詞游戲

    python實現(xiàn)簡單猜單詞游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單猜單詞游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 詳解Python中import機制

    詳解Python中import機制

    這篇文章主要介紹了Python中import機制的相關(guān)資料,幫助大家更好的理解和學(xué)習python,感興趣的朋友可以了解下
    2020-09-09
  • Python實現(xiàn)12種降維算法的示例代碼

    Python實現(xiàn)12種降維算法的示例代碼

    數(shù)據(jù)降維算法是機器學(xué)習算法中的大家族,與分類、回歸、聚類等算法不同,它的目標是將向量投影到低維空間,以達到某種目的如可視化,或是做分類。本文將利用Python實現(xiàn)12種降維算法,需要的可以參考一下
    2022-04-04
  • pytorch中tensor的合并與截取方法

    pytorch中tensor的合并與截取方法

    今天小編就為大家分享一篇pytorch中tensor的合并與截取方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python實現(xiàn)爬取逐浪小說的方法

    Python實現(xiàn)爬取逐浪小說的方法

    這篇文章主要介紹了Python實現(xiàn)爬取逐浪小說的方法,基于Python的正則匹配功能實現(xiàn)爬取小說頁面標題、鏈接及正文等功能,需要的朋友可以參考下
    2015-07-07
  • python操作excel讓工作自動化

    python操作excel讓工作自動化

    這篇文章主要為大家詳細介紹了python如何操作excel讓工作自動化,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • keras自動編碼器實現(xiàn)系列之卷積自動編碼器操作

    keras自動編碼器實現(xiàn)系列之卷積自動編碼器操作

    這篇文章主要介紹了keras自動編碼器實現(xiàn)系列之卷積自動編碼器操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Pytorch?使用Google?Colab訓(xùn)練神經(jīng)網(wǎng)絡(luò)深度學(xué)習

    Pytorch?使用Google?Colab訓(xùn)練神經(jīng)網(wǎng)絡(luò)深度學(xué)習

    本文以VOC數(shù)據(jù)集為例,因此在訓(xùn)練的時候沒有修改classes_path等,如果是訓(xùn)練自己的數(shù)據(jù)集,各位一定要注意修改classes_path等其它參數(shù)
    2022-04-04
  • Python基于回溯法子集樹模板解決選排問題示例

    Python基于回溯法子集樹模板解決選排問題示例

    這篇文章主要介紹了Python基于回溯法子集樹模板解決選排問題,簡單描述了選排問題并結(jié)合實例形式分析了Python使用回溯法子集樹模板解決選排問題的具體實現(xiàn)步驟與相關(guān)操作注意事項,需要的朋友可以參考下
    2017-09-09
  • PyQt5 實現(xiàn)狀態(tài)欄永久顯示消息

    PyQt5 實現(xiàn)狀態(tài)欄永久顯示消息

    這篇文章主要介紹了PyQt5 實現(xiàn)狀態(tài)欄永久顯示消息的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

铁岭市| 南溪县| 雷山县| 正蓝旗| 徐水县| 黑山县| 兴海县| 奎屯市| 哈密市| 长乐市| 宝丰县| 吴川市| 阳谷县| 长治县| 渝中区| 桃园县| 高台县| 翁源县| 金湖县| 会昌县| 哈密市| 保德县| 郧西县| 德令哈市| 精河县| 黎城县| 宁德市| 盐山县| 什邡市| 林州市| 明溪县| 沂水县| 拉孜县| 大洼县| 泽普县| 巴南区| 通辽市| 洛南县| 毕节市| 长汀县| 宝兴县|