python學習-學生信息管理系統並打包exe

来源:https://www.cnblogs.com/chenlei53/archive/2023/04/22/17278944.html
-Advertisement-
Play Games

在B站自學Python 站主:Python_子木 授課:楊淑娟 平臺: 馬士兵教育 python: 3.9.9 #python打包exe文件 #安裝PyInstaller pip install PyInstaller #-F打包exe文件,stusystem\stusystem.py到py的路徑, ...


在B站自學Python
站主:Python_子木
授課:楊淑娟
平臺: 馬士兵教育
python: 3.9.9

python打包exe文件

#安裝PyInstaller
pip install PyInstaller
#-F打包exe文件,stusystem\stusystem.py到py的路徑,可以是絕對路徑,可以是相對路徑
pyinstaller -F stusystem\stusystem.py

學生信息管理系統具體代碼如下

import os.path
filename='student.txt'
def main():
    while True:
        menu()
        choice = int(input('請選擇'))
        if choice in range(8):
            if choice==0:
                answer=input('您確定要退出系統嗎?y/n')
                if answer.lower()=='y':
                    print('謝謝您的使用!')
                    break #退出系統
                else:
                    continue
            if choice==1:
                insert()
            if choice==2:
                search()
            if choice==3:
                delete()
            if choice==4:
                modify()
            if choice==5:
                sort()
            if choice==6:
                total()
            if choice==7:
                show()
        else:
            print('無此功能,請按菜單重新選擇')
def menu():
    print('=========================學生信息管理系統============================')
    print('-----------------------------功能菜單------------------------------')
    print('\t\t\t\t\t\t1.錄入學生信息')
    print('\t\t\t\t\t\t2.查找學生信息')
    print('\t\t\t\t\t\t3.刪除學生信息')
    print('\t\t\t\t\t\t4.修改學生信息')
    print('\t\t\t\t\t\t5.排序')
    print('\t\t\t\t\t\t6.統計學生總人數')
    print('\t\t\t\t\t\t7.顯示所有學生信息')
    print('\t\t\t\t\t\t0.退出')
    print('------------------------------------------------------------------')

def insert():
    student_list=[]
    while True:
        id=input('請輸入ID(如1001):')
        if not id:
            break
        name=input('請輸入姓名:')
        if not name:
            break
        try:
            english=int(input('請輸入英語成績:'))
            python=int(input('請輸入Python成績:'))
            java=int(input('請輸入Java成績:'))
        except:
            print('輸入無效,不是整數類型,請重新輸入')
            continue
        #將錄入的學生保存到字典中
        student={'id':id,'name':name,'english':english,'python':python,'java':java}
        #將學生信息添加到列表中
        student_list.append(student)
        answer=input('是否繼續添加?y/n')
        if answer.lower()=='y':
            continue
        else:
            break

    #調用save()函數
    save(student_list)
    print('學生信息錄入完畢!!!')

def save(lst):
    try:
        stu_txt=open(filename,'a',encoding='utf-8')
    except:
        stu_txt=open(filename,'w',encoding='utf-8')
    for item in lst:
        stu_txt.write(str(item)+'\n')
    stu_txt.close()

def search():
    student_query=[]
    while True:
        id=''
        name=''
        if os.path.exists(filename):
            mode=input('按ID查找請輸入1,按姓名查找請輸入2:')
            if mode=='1':
                id=input('請輸入學生ID:')
            elif mode=='2':
                name=input('請輸入學生姓名:')
            else:
                print('您輸入有誤,請重新輸入')
                continue
            with open(filename,'r',encoding='utf-8') as rfile:
                student=rfile.readlines()
                for item in student:
                    d=dict(eval(item))
                    if id!='':
                        if d['id']==id:
                            student_query.append(d)
                    elif name!='':
                        if d['name']==name:
                            student_query.append(d)
            #顯示查詢結果
            show_student(student_query)
            #清空列表
            student_query.clear()
            answer=input('是否要繼續查詢?y/n\n')
            if answer.lower()=='y':
                continue
            else:
                break
        else:
            print('暫未保存學生信息')
            return
def show_student(lst):
    if len(lst)==0:
        print('沒有查詢到學生信息,無數據顯示!!!')
        return
    #定義標題顯示格式
    format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
    print(format_title.format('ID','姓名','英語成績','Python成績','Java成績','總成績'))
    #定義內容顯示格式
    format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
    for item in lst:
        print(format_data.format(item.get('id'),
                                 item.get('name'),
                                 item.get('english'),
                                 item.get('python'),
                                 item.get('java'),
                                 int(item.get('english'))+int(item.get('python'))+int(item.get('java'))
                                 ))


def delete():
    while True:
        student_id=input('請輸入要刪除的學生的ID:')
        if student_id!='':
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as file:
                    student_old=file.readlines()
            else:
                student_old=[]
            flag=False #刪除標記
            if student_old:
                with open(filename,'w',encoding='utf-8') as wfile:
                    d={}
                    for item in student_old:
                        d=dict(eval(item)) #將字元串轉字典
                        if d['id']!=student_id:
                            wfile.write(str(d)+'\n')
                        else:
                            flag=True
                    if flag:
                        print(f'id為{student_id}的學生信息已被刪除')
                    else:
                        print(f'沒有找到ID為{student_id}的學生信息')
            else:
                print('無學生信息')
                break
            show() #刪除之後要重新顯示所有學生信息
            answer=input('是否繼續刪除?y/n')
            if answer.lower()=='y':
                continue
            else:
                break

def modify():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            student_old=rfile.readlines()
    else:
        return
    student_id=input('請輸入要修改的學員的ID:')
    with open(filename,'w',encoding='utf-8') as wfile:
        for item in student_old:
            d=dict(eval(item))
            if d['id']==student_id:
                print('找到學生信息,可以修改他的相關信息了!')
                while True:
                    try:
                        d['name']=input('請輸入姓名:')
                        d['english']=input('請輸入英語成績:')
                        d['python']=input('請輸入python成績:')
                        d['java']=input('請輸入java成績:')
                    except:
                        print('您的輸入有誤,請重新輸入!!!')
                    else:
                        break
                wfile.write(str(d)+'\n')
                print('修改成功!')
                show()
            else:
                print(f'未找到ID為{student_id}的學生信息')
                wfile.write(str(d)+'\n')
        answer=input('是否繼續修改其他學生信息?y/n\n')
        if answer.lower()=='y':
            modify()

def sort():
    show()
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            student_list=rfile.readlines()
        student_new=[]
        for item in student_list:
            d=dict(eval(item))
            student_new.append(d)

    else:
        return
    asc_or_desc=input('請選擇(0.升序,1.降序):')
    if asc_or_desc=='0':
        asc_or_desc_bool=False
    elif asc_or_desc=='1':
        asc_or_desc_bool=True
    else:
        print('您的輸入有誤,請重新輸入')
        sort()
    mode=input('請選擇排序方式(1.按英語成績排序 2.按Python成績排序 3.按Java成績排序 4.按總成績排序)')
    if mode=='1':
        student_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)
    elif mode=='2':
        student_new.sort(key=lambda x:int(x['python']),reverse=asc_or_desc_bool)
    elif mode=='3':
        student_new.sort(key=lambda x:int(x['java']),reverse=asc_or_desc_bool)
    elif mode=='4':
        student_new.sort(key=lambda x:int(x['english'])+int(x['python'])+int(x['java']),reverse=asc_or_desc_bool)
    else:
        print('您的輸入有誤,請重新輸入')
        sort()
    show_student(student_new)
def total():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            students=rfile.readlines()
            if students:
                print(f'一共有{len(students)}名學生')
            else:
                print('還沒有錄入學生信息')
    else:
        print('暫未保存數據信息...')
def show():
    student_list = []
    if os.path.exists(filename):
        # 定義標題顯示格式
        format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
        print(format_title.format('ID', '姓名', '英語成績', 'Python成績', 'Java成績', '總成績'))
        with open(filename,'r',encoding='utf-8') as rfile:
            student = rfile.readlines()
            for item in student:
                d = dict(eval(item))
                student_list.append(d)
        if len(student_list)==0:
            format_nodata = '{:^6}'
            print(format_nodata.format('無數據'))
        # 定義內容顯示格式
        format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
        for item in student_list:
            print(format_data.format(item.get('id'),
                                    item.get('name'),
                                    item.get('english'),
                                    item.get('python'),
                                    item.get('java'),
                                    int(item.get('english')) + int(item.get('python')) + int(item.get('java'))
                                    ))
        student_list.clear()
    else:
        print('暫未保存過數據!!!')


if __name__ == '__main__':
    main()

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 說明 使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇對 VLD 源碼包中的各文件用途做個概述。同系列文章目錄可見 《記憶體泄漏檢測工具》目錄 1. 整體概覽 以 vld2.5.1 版本為例,下載源碼 後,根目錄下一共 5 個文件夾:.teamcity、lib、mfc_detect、set ...
  • 創建一個成績單文件score.xlsx,將平時成績單.xlsx文件中對應班級工作表中學號和姓名列的內容寫入到score.xlsx中,並添加成績列,每個學生的成績採用隨機生成的一個分數填寫進去,最後統計所有學生的平均成績計算出來後,寫入到score.xlsx的最後一行最後一列之後的單元格中去。預想的步 ...
  • 本文首發於公眾號:Hunter後端 原文鏈接:Django筆記二十八之資料庫查詢優化彙總 這一篇筆記將從以下幾個方面來介紹 Django 在查詢過程中的一些優化操作,有一些是介紹如何獲取 Django 查詢轉化的 sql 語句,有一些是理解 QuerySet 是如何獲取數據的。 以下是本篇筆記目錄: ...
  • #一:什麼是多線程 線程是操作系統能夠進行運算調度的最小單位;它被包含在進程之中,是進程中的實際運作單位。 多線程,是指從軟體或者硬體上實現多個線程併發執行的技術。具有多線程能力的電腦因有硬體支持而能夠在同一時間執行多於一個線程,進而提升整體處理性能。 簡單來說:線程是程式中一個單一的順序控制流程 ...
  • Springboot 多實例負載均衡部署 一、測試代碼: 控制層測試代碼: import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; @Controller @Re ...
  • Midjourney,是一個革命性的基於人工智慧的藝術生成器,可以從被稱為提示的簡單文本描述中生成令人驚嘆的圖像。Midjourney已經迅速成為藝術家、設計師和營銷人員的首選工具(包括像我這樣根本不會設計任何東西的無能之輩)。 為了幫助你開始使用這個強大的工具,我們彙編了一份15個資源的清單,可以 ...
  • template<typename T = CString, typename _Data = CString> struct Union_node//!< 節點 { Union_node() :nColor(0) {} std::vector<Union_node*> vecNodeSon; T ...
  • Python基礎—conda使用筆記 1. 環境配置 由於用conda管理虛擬環境真滴很方便,所以主要使用conda,就不單獨去裝Python了。 1.1. Miniconda3安裝 Miniconda3官網下載地址:Miniconda Miniconda3清華鏡像下載:清華鏡像-Miniconda ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...