python代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
本文實(shí)例為大家分享了python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的具體代碼,含代碼注釋、增刪改查、排序、統(tǒng)計(jì)顯示學(xué)生信息,供大家參考,具體內(nèi)容如下
運(yùn)行如下:
具體代碼如下:
# _*_ coding:utf-8 ? _*_ import re ?# 導(dǎo)入正則表達(dá)式模塊 import os ?# 導(dǎo)入操作系統(tǒng)模塊 ? filename = "students.txt" ?# 定義保存學(xué)生信息的文件名 ? def menu(): ? ? print("------------------------學(xué)生管理系統(tǒng)------------------------") ? ? print('\t\t\t1.錄入學(xué)生信息') ? ? print('\t\t\t2.查找學(xué)生信息') ? ? print('\t\t\t3.刪除學(xué)生信息') ? ? print('\t\t\t4.修改學(xué)生信息') ? ? print('\t\t\t5.排序') ? ? print('\t\t\t6.統(tǒng)計(jì)學(xué)生總?cè)藬?shù)') ? ? print('\t\t\t7.顯示所有學(xué)生信息') ? ? print('\t\t\t0.退出') ? ? print('------------------------------------------------------------') ? ? def main(): ? ? ctrl = True ?# 標(biāo)記是否退出系統(tǒng) ? ? while (ctrl): ? ? ? ? menu() ?# 顯示菜單 ? ? ? ? option = input("請選擇:") ?# 選擇菜單項(xiàng) ? ? ? ? option_str = re.sub("\D", "", option) ?# 提取數(shù)字 ? ? ? ? if option_str in ['0', '1', '2', '3', '4', '5', '6', '7']: ? ? ? ? ? ? option_int = int(option_str) ? ? ? ? ? ? if option_int == 0: ?# 退出系統(tǒng) ? ? ? ? ? ? ? ? print('您已退出學(xué)生成績管理系統(tǒng)!') ? ? ? ? ? ? ? ? ctrl = False ? ? ? ? ? ? elif option_int == 1: ?# 錄入學(xué)生成績信息 ? ? ? ? ? ? ? ? insert() ? ? ? ? ? ? elif option_int == 2: ?# 查找學(xué)生成績信息 ? ? ? ? ? ? ? ? search() ? ? ? ? ? ? elif option_int == 3: ?# 刪除學(xué)生成績信息 ? ? ? ? ? ? ? ? delete() ? ? ? ? ? ? elif option_int == 4: ?# 修改學(xué)生成績信息 ? ? ? ? ? ? ? ? modify() ? ? ? ? ? ? elif option_int == 5: ?# 排序 ? ? ? ? ? ? ? ? sort() ? ? ? ? ? ? elif option_int == 6: ?# 統(tǒng)計(jì)學(xué)生總數(shù) ? ? ? ? ? ? ? ? total() ? ? ? ? ? ? elif option_int == 7: ?# 顯示所有學(xué)生信息 ? ? ? ? ? ? ? ? show() ? '''1 錄入學(xué)生信息''' ? def insert(): ? ? stdentList = [] ? ? ? ?# 保存學(xué)生信息的列表 ? ? mark = True ?# 是否繼續(xù)添加 ? ? while mark: ? ? ? ? id = input("請輸入ID(如 1001):") ? ? ? ? if not id: ?# ID為空,跳出循環(huán) ? ? ? ? ? ? break ? ? ? ? name = input("請輸入名字:") ? ? ? ? if not name: ?# 名字為空,跳出循環(huán) ? ? ? ? ? ? break ? ? ? ? try: ? ? ? ? ? ? english = int(input("請輸入英語成績:")) ? ? ? ? ? ? python = int(input("請輸入Python成績:")) ? ? ? ? ? ? c = int(input("請輸入C語言成績:")) ? ? ? ? except: ? ? ? ? ? ? print("輸入無效,不是整型數(shù)值....重新錄入信息") ? ? ? ? ? ? continue ? ? ? ? stdent = {"id": id, "name": name, "english": english, "python": python, "c": c} ?# 將輸入的學(xué)生信息保存到字典 ? ? ? ? stdentList.append(stdent) ?# 將學(xué)生字典添加到列表中 ? ? ? ? inputMark = input("是否繼續(xù)添加?(y/n):") ? ? ? ? if inputMark == "y": ?# 繼續(xù)添加 ? ? ? ? ? ? mark = True ? ? ? ? else: ?# 不繼續(xù)添加 ? ? ? ? ? ? mark = False ? ? save(stdentList) ?# 將學(xué)生信息保存到文件 ? ? print("學(xué)生信息錄入完畢?。?!") ? # 將學(xué)生信息保存到文件 def save(student): ? ? try: ? ? ? ? students_txt = open(filename, "a") ?# 以追加模式打開 ? ? except Exception as e: ? ? ? ? students_txt = open(filename, "w") ?# 文件不存在,創(chuàng)建文件并打開 ? ? for info in student: ? ? ? ? students_txt.write(str(info) + "\n") ?# 按行存儲(chǔ),添加換行符 ? ? students_txt.close() ?# 關(guān)閉文件 ? '''2 查找學(xué)生成績信息''' ? def search(): ? ? mark = True ? ? student_query = [] ?# 保存查詢結(jié)果的學(xué)生列表 ? ? while mark: ? ? ? ? id = "" ? ? ? ? name = "" ? ? ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? ? ? mode = input("按ID查輸入1;按姓名查輸入2:") ? ? ? ? ? ? if mode == "1": ? ? ? ? ? ? ? ? id = input("請輸入學(xué)生ID:") ? ? ? ? ? ? elif mode == "2": ? ? ? ? ? ? ? ? name = input("請輸入學(xué)生姓名:") ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? print("您的輸入有誤,請重新輸入!") ? ? ? ? ? ? ? ? search() ?# 重新查詢 ? ? ? ? ? ? with open(filename, 'r') as file: ?# 打開文件 ? ? ? ? ? ? ? ? student = file.readlines() ?# 讀取全部內(nèi)容 ? ? ? ? ? ? ? ? for list in student: ? ? ? ? ? ? ? ? ? ? d = dict(eval(list)) ?# 字符串轉(zhuǎn)字典 ? ? ? ? ? ? ? ? ? ? if id is not "": ?# 判斷是否按ID查 ? ? ? ? ? ? ? ? ? ? ? ? if d['id'] == id: ? ? ? ? ? ? ? ? ? ? ? ? ? ? student_query.append(d) ?# 將找到的學(xué)生信息保存到列表中 ? ? ? ? ? ? ? ? ? ? elif name is not "": ?# 判斷是否按姓名查 ? ? ? ? ? ? ? ? ? ? ? ? if d['name'] == name: ? ? ? ? ? ? ? ? ? ? ? ? ? ? student_query.append(d) ?# 將找到的學(xué)生信息保存到列表中 ? ? ? ? ? ? ? ? show_student(student_query) ?# 顯示查詢結(jié)果 ? ? ? ? ? ? ? ? student_query.clear() ?# 清空列表 ? ? ? ? ? ? ? ? inputMark = input("是否繼續(xù)查詢?(y/n):") ? ? ? ? ? ? ? ? if inputMark == "y": ? ? ? ? ? ? ? ? ? ? mark = True ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? mark = False ? ? ? ? else: ? ? ? ? ? ? print("暫未保存數(shù)據(jù)信息...") ? ? ? ? ? ? return ? '''3 刪除學(xué)生成績信息''' ? def delete(): ? ? mark = True ?# 標(biāo)記是否循環(huán) ? ? while mark: ? ? ? ? studentId = input("請輸入要?jiǎng)h除的學(xué)生ID:") ? ? ? ? if studentId is not "": ?# 判斷要?jiǎng)h除的學(xué)生是否存在 ? ? ? ? ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? ? ? ? ? with open(filename, 'r') as rfile: ?# 打開文件 ? ? ? ? ? ? ? ? ? ? student_old = rfile.readlines() ?# 讀取全部內(nèi)容 ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? student_old = [] ? ? ? ? ? ? ifdel = False ?# 標(biāo)記是否刪除 ? ? ? ? ? ? if student_old: ?# 如果存在學(xué)生信息 ? ? ? ? ? ? ? ? with open(filename, 'w') as wfile: ?# 以寫方式打開文件 ? ? ? ? ? ? ? ? ? ? d = {} ?# 定義空字典 ? ? ? ? ? ? ? ? ? ? for list in student_old: ? ? ? ? ? ? ? ? ? ? ? ? d = dict(eval(list)) ?# 字符串轉(zhuǎn)字典 ? ? ? ? ? ? ? ? ? ? ? ? if d['id'] != studentId: ? ? ? ? ? ? ? ? ? ? ? ? ? ? wfile.write(str(d) + "\n") ?# 將一條學(xué)生信息寫入文件 ? ? ? ? ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ifdel = True ?# 標(biāo)記已經(jīng)刪除 ? ? ? ? ? ? ? ? ? ? if ifdel: ? ? ? ? ? ? ? ? ? ? ? ? print("ID為 %s 的學(xué)生信息已經(jīng)被刪除..." % studentId) ? ? ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? ? ? print("沒有找到ID為 %s 的學(xué)生信息..." % studentId) ? ? ? ? ? ? else: ?# 不存在學(xué)生信息 ? ? ? ? ? ? ? ? print("無學(xué)生信息...") ? ? ? ? ? ? ? ? break ?# 退出循環(huán) ? ? ? ? ? ? show() ?# 顯示全部學(xué)生信息 ? ? ? ? ? ? inputMark = input("是否繼續(xù)刪除?(y/n):") ? ? ? ? ? ? if inputMark == "y": ? ? ? ? ? ? ? ? mark = True ?# 繼續(xù)刪除 ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? mark = False ?# 退出刪除學(xué)生信息功能 ? '''4 修改學(xué)生成績信息''' ? def modify(): ? ? show() ?# 顯示全部學(xué)生信息 ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? with open(filename, 'r') as rfile: ?# 打開文件 ? ? ? ? ? ? student_old = rfile.readlines() ?# 讀取全部內(nèi)容 ? ? else: ? ? ? ? return ? ? studentid = input("請輸入要修改的學(xué)生ID:") ? ? with open(filename, "w") as wfile: ?# 以寫模式打開文件 ? ? ? ? for student in student_old: ? ? ? ? ? ? d = dict(eval(student)) ?# 字符串轉(zhuǎn)字典 ? ? ? ? ? ? if d["id"] == studentid: ?# 是否為要修改的學(xué)生 ? ? ? ? ? ? ? ? print("找到了這名學(xué)生,可以修改他的信息!") ? ? ? ? ? ? ? ? while True: ?# 輸入要修改的信息 ? ? ? ? ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? ? ? ? ? d["name"] = input("請輸入姓名:") ? ? ? ? ? ? ? ? ? ? ? ? d["english"] = int(input("請輸入英語成績:")) ? ? ? ? ? ? ? ? ? ? ? ? d["python"] = int(input("請輸入Python成績:")) ? ? ? ? ? ? ? ? ? ? ? ? d["c"] = int(input("請輸入C語言成績:")) ? ? ? ? ? ? ? ? ? ? except: ? ? ? ? ? ? ? ? ? ? ? ? print("您的輸入有誤,請重新輸入。") ? ? ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? ? ? break ?# 跳出循環(huán) ? ? ? ? ? ? ? ? student = str(d) ?# 將字典轉(zhuǎn)換為字符串 ? ? ? ? ? ? ? ? wfile.write(student + "\n") ? # 將修改的信息寫入到文件 ? ? ? ? ? ? ? ? print("修改成功!") ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? wfile.write(student) ?# 將未修改的信息寫入到文件 ? ? mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):") ? ? if mark == "y": ? ? ? ? modify() ?# 重新執(zhí)行修改操作 ? '''5 排序''' ? def sort(): ? ? show() ?# 顯示全部學(xué)生信息 ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? with open(filename, 'r') as file: ?# 打開文件 ? ? ? ? ? ? student_old = file.readlines() ?# 讀取全部內(nèi)容 ? ? ? ? ? ? student_new = [] ? ? ? ? for list in student_old: ? ? ? ? ? ? d = dict(eval(list)) ?# 字符串轉(zhuǎn)字典 ? ? ? ? ? ? student_new.append(d) ?# 將轉(zhuǎn)換后的字典添加到列表中 ? ? else: ? ? ? ? return ? ? ascORdesc = input("請選擇(0升序;1降序):") ? ? if ascORdesc == "0": ?# 按升序排序 ? ? ? ? ascORdescBool = False ? ? ? ? ? # 標(biāo)記變量,為False表示升序排序 ? ? elif ascORdesc == "1": ?# 按降序排序 ? ? ? ? ascORdescBool = True ? ? ? ? ?# 標(biāo)記變量,為True表示降序排序 ? ? else: ? ? ? ? print("您的輸入有誤,請重新輸入!") ? ? ? ? sort() ? ? ? mode = input("請選擇排序方式(1按英語成績排序;2按Python成績排序;3按C語言成績排序;0按總成績排序):") ? ? if mode == "1": ?# 按英語成績排序 ? ? ? ? student_new.sort(key=lambda x: x["english"], reverse=ascORdescBool) ? ? elif mode == "2": ?# 按Python成績排序 ? ? ? ? student_new.sort(key=lambda x: x["python"], reverse=ascORdescBool) ? ? elif mode == "3": ?# 按C語言成績排序 ? ? ? ? student_new.sort(key=lambda x: x["c"], reverse=ascORdescBool) ? ? elif mode == "0": ?# 按總成績排序 ? ? ? ? student_new.sort(key=lambda x: x["english"] + x["python"] + x["c"], reverse=ascORdescBool) ? ? else: ? ? ? ? print("您的輸入有誤,請重新輸入!") ? ? ? ? sort() ? ? show_student(student_new) ?# 顯示排序結(jié)果 ? ''' 6 統(tǒng)計(jì)學(xué)生總數(shù)''' ? def total(): ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? with open(filename, 'r') as rfile: ?# 打開文件 ? ? ? ? ? ? student_old = rfile.readlines() ?# 讀取全部內(nèi)容 ? ? ? ? ? ? if student_old: ? ? ? ? ? ? ? ? print("一共有 %d 名學(xué)生!" % len(student_old)) ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? print("還沒有錄入學(xué)生信息!") ? ? else: ? ? ? ? print("暫未保存數(shù)據(jù)信息...") ? ''' 7 顯示所有學(xué)生信息 ''' ? def show(): ? ? student_new = [] ? ? if os.path.exists(filename): ?# 判斷文件是否存在 ? ? ? ? with open(filename, 'r') as rfile: ?# 打開文件 ? ? ? ? ? ? student_old = rfile.readlines() ?# 讀取全部內(nèi)容 ? ? ? ? for list in student_old: ? ? ? ? ? ? student_new.append(eval(list)) ?# 將找到的學(xué)生信息保存到列表中 ? ? ? ? if student_new: ? ? ? ? ? ? show_student(student_new) ? ? else: ? ? ? ? print("暫未保存數(shù)據(jù)信息...") ? ? # 將保存在列表中的學(xué)生信息顯示出來 def show_student(studentList): ? ? if not studentList: ? ? ? ? print("(o@.@o) 無數(shù)據(jù)信息 (o@.@o) \n") ? ? ? ? return ? ? format_title = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}" ? ? print(format_title.format("ID", "名字", "英語成績", "Python成績", "C語言成績", "總成績")) ? ? format_data = "{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}\t{:^12}" ? ? for info in studentList: ? ? ? ? print(format_data.format(info.get("id"), info.get("name"), str(info.get("english")), str(info.get("python")), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?str(info.get("c")), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?str(info.get("english") + info.get("python") + info.get("c")).center(12))) ? ? if __name__ == "__main__": ? ? main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的示例代碼
- 基于Python實(shí)現(xiàn)一個(gè)簡單的學(xué)生管理系統(tǒng)
- 基于Python實(shí)現(xiàn)簡易學(xué)生信息管理系統(tǒng)
- python實(shí)現(xiàn)簡易的學(xué)生信息管理系統(tǒng)
- python超詳細(xì)實(shí)現(xiàn)完整學(xué)生成績管理系統(tǒng)
- Python實(shí)現(xiàn)簡單的學(xué)生信息管理系統(tǒng)
- Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的示例代碼
相關(guān)文章
Python?range函數(shù)生成一系列連續(xù)整數(shù)的內(nèi)部機(jī)制解析
這篇文章主要為大家介紹了Python?range函數(shù)生成一系列連續(xù)整數(shù)的內(nèi)部機(jī)制解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12python列表推導(dǎo)式實(shí)現(xiàn)找出列表中長度大于5的名字
這篇文章主要介紹了python列表推導(dǎo)式實(shí)現(xiàn)找出列表中長度大于5的名字,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python實(shí)現(xiàn)PyPDF2處理PDF文件的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)PyPDF2處理PDF文件的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Pycharm配置Qt Designer及Pyuic的實(shí)現(xiàn)方法
本文介紹了如何安裝Qt designer和Pyuic以及他們的基本用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07基于Python輕松實(shí)現(xiàn)PDF轉(zhuǎn)圖片
PDF文件是我們在日常工作和學(xué)習(xí)中常用的文檔格式之一,但你知道嗎,你可以將PDF文件轉(zhuǎn)換為圖像,讓文檔變得更加生動(dòng)有趣,下面我們就來看看具體的實(shí)現(xiàn)方法吧2023-08-08Python3 Tkinter選擇路徑功能的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python3 Tkinter選擇路徑功能的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python中定時(shí)器用法詳解之Timer定時(shí)器和schedule庫
目前所在的項(xiàng)目組需要經(jīng)常執(zhí)行一些定時(shí)任務(wù),于是選擇使用 Python 的定時(shí)器,下面這篇文章主要給大家介紹了關(guān)于Python中定時(shí)器用法詳解之Timer定時(shí)器和schedule庫的相關(guān)資料,需要的朋友可以參考下2024-02-02