python實(shí)現(xiàn)簡易學(xué)生信息管理系統(tǒng)
本文實(shí)例為大家分享了python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
簡易學(xué)生信息管理系統(tǒng)主要功能有
1 錄入學(xué)生信息
2 查找學(xué)生信息
3 刪除學(xué)生信息
4 修改學(xué)生信息
5 排序
6 統(tǒng)計(jì)學(xué)生總?cè)藬?shù)
7 顯示所有學(xué)生信息
0 退出系統(tǒng)
系統(tǒng)運(yùn)行效果
主菜單的代碼方法:
# Author: dry # 開發(fā)時間:2019/9/11 # 開發(fā)工具:PyCharm import re # 導(dǎo)入正則表達(dá)式模塊 import os # 導(dǎo)入操作系統(tǒng)模塊 filename = "student.txt" # 學(xué)生信息保存文件 def menu(): # 輸出菜單 print(''' ---------------學(xué)生信息管理系統(tǒng)------------ ==================功能菜單================ 1 錄入學(xué)生信息 2 查找學(xué)生信息 3 刪除學(xué)生信息 4 修改學(xué)生信息 5 排序 6 統(tǒng)計(jì)學(xué)生總?cè)藬?shù) 7 顯示所有學(xué)生信息 0 退出系統(tǒng) ======================================= 說明:通過數(shù)字選擇菜單 ======================================= ''')
系統(tǒng)主方法:
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()
錄入學(xué)生信息方法:
'''錄入學(xué)生信息''' def insert(): studentList = [] # 保存學(xué)生信息的列表 mark = True # 是否繼續(xù)添加 while mark: id = input("請輸入學(xué)生ID(如1001):") if not id: break name = input("請輸入名字:") if not name: break try: english = int(input("請輸入英語成績:")) python = int(input("請輸入python成績:")) c = int(input("請輸入C語言成績:")) except: print("輸入無效,不是整型數(shù)值,請重新輸入信息") continue # 將輸入的學(xué)生信息保存到字典 student = {"id": id, "name": name, "english": english, "python": python, "c": c} studentList.append(student) # 將學(xué)生字典添加到列表中 inputList = input("是否繼續(xù)添加?(y/n):") if inputList == 'y': # 繼續(xù)添加 mark = True else: mark = False save(studentList) # 將學(xué)生信息保存到文件 print("學(xué)生信息錄入完畢!!!")
保存學(xué)生信息方法:
'''將學(xué)生信息保存到文件''' def save(student): try: student_txt = open(filename, 'a') # 以追加模式打開 except Exception as e: student_txt = open(filename, 'w') # 文件不存在,創(chuàng)建文件并打開 for info in student: student_txt.write(str(info) + "\n") # 執(zhí)行存儲,添加換行符 student_txt.close() # 關(guān)閉文件
查詢學(xué)生信息方法:
'''查詢學(xué)生信息''' def search(): mark = True student_query = [] 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() for list in student: d = dict(eval(list)) if id is not "": if d['id'] == id: student_query.append(d) elif name is not "": if d['name'] == name: student_query.append(d) show_student(student_query) student_query.clear() inputMark = input("是否繼續(xù)查詢?(y/n):") if inputMark == "y": mark = True else: mark = False else: print("暫未保存數(shù)據(jù)信息...") return
顯示學(xué)生信息方法
'''將保存在列表中的學(xué)生信息顯示出來''' def show_student(studentList): if not studentList: print("無效的數(shù)據(jù)") 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{:^10}\t{:^10}\t{:^10}\t{:10}" 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)))
刪除學(xué)生信息方法:
'''刪除學(xué)生信息''' def delete(): mark = True # 標(biāo)記是否循環(huán) while mark: studentId = input("請輸入要刪除的學(xué)生ID:") if studentId is not "": # 判斷要刪除的學(xué)生ID是否存在 if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() 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") # 將一條信息寫入文件 else: ifdel = True # 標(biāo)記已經(jīng)刪除 if ifdel: print("ID為%s的學(xué)生信息已經(jīng)被刪除..." % studentId) else: print("沒有找到ID為%s的學(xué)生信息..." % studentId) else: print("不存在學(xué)生信息") break show() # 顯示全部學(xué)生信息 inputMark = input("是否繼續(xù)刪除?(y/n):") if inputMark == "y": mark = True # 繼續(xù)刪除 else: mark = False # 退出刪除學(xué)生信息操作
修改學(xué)生信息方法:
'''修改學(xué)生信息''' def modify(): show() # 顯示全部學(xué)生信息 if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() else: return studentid = input("請輸入要修改的學(xué)生ID:") with open(filename, 'w') as wfile: for student in student_old: d = dict(eval(student)) if d['id'] == studentid: 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 student = str(d) # 將字典轉(zhuǎn)為字符串 wfile.write(student + "\n") # 將修改信息寫入到文件 print("修改成功") else: wfile.write(student) # 將未修改的信息寫入文件 mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):") if mark == "y": modify()
學(xué)生信息排序方法:
'''排序''' def sort(): show() if os.path.exists(filename): with open(filename, 'r') as file: student_old = file.readlines() student_new = [] for list in student_old: d = dict(eval(list)) student_new.append(d) else: return ascORdesc = input("請選擇(0升序;1降序)") if ascORdesc == "0": ascORdescBool = False # 標(biāo)記變量,為False時表示升序,為True時表示降序 elif ascORdesc == "1": ascORdescBool = 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é)果
統(tǒng)計(jì)學(xué)生總數(shù)方法:
'''統(tǒng)計(jì)學(xué)生總數(shù)''' def total(): if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() if student_old: print("一共有%d名學(xué)生!" % len(student_old)) else: print("還沒有錄入學(xué)生信息") else: print("暫未保存數(shù)據(jù)信息")
顯示所有學(xué)生信息方法:
'''顯示所有學(xué)生信息''' def show(): student_new = [] if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() for list in student_old: student_new.append(eval(list)) if student_new: show_student(student_new) else: print("暫未保存數(shù)據(jù)信息")
開始函數(shù):
if __name__ == '__main__': main()
完整代碼如下:
# Author: dry # 開發(fā)時間:2019/9/11 # 開發(fā)工具:PyCharm import re # 導(dǎo)入正則表達(dá)式模塊 import os # 導(dǎo)入操作系統(tǒng)模塊 filename = "student.txt" # 學(xué)生信息保存文件 def menu(): # 輸出菜單 print(''' ---------------學(xué)生信息管理系統(tǒng)------------ ==================功能菜單================ 1 錄入學(xué)生信息 2 查找學(xué)生信息 3 刪除學(xué)生信息 4 修改學(xué)生信息 5 排序 6 統(tǒng)計(jì)學(xué)生總?cè)藬?shù) 7 顯示所有學(xué)生信息 0 退出系統(tǒng) ======================================= 說明:通過數(shù)字選擇菜單 ======================================= ''') 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() '''錄入學(xué)生信息''' def insert(): studentList = [] # 保存學(xué)生信息的列表 mark = True # 是否繼續(xù)添加 while mark: id = input("請輸入學(xué)生ID(如1001):") if not id: break name = input("請輸入名字:") if not name: break try: english = int(input("請輸入英語成績:")) python = int(input("請輸入python成績:")) c = int(input("請輸入C語言成績:")) except: print("輸入無效,不是整形數(shù)值,請重新輸入信息") continue # 將輸入的學(xué)生信息保存到字典 student = {"id": id, "name": name, "english": english, "python": python, "c": c} studentList.append(student) # 將學(xué)生字典添加到列表中 inputList = input("是否繼續(xù)添加?(y/n):") if inputList == 'y': # 繼續(xù)添加 mark = True else: mark = False save(studentList) # 將學(xué)生信息保存到文件 print("學(xué)生信息錄入完畢!!!") '''將學(xué)生信息保存到文件''' def save(student): try: student_txt = open(filename, 'a') # 以追加模式打開 except Exception as e: student_txt = open(filename, 'w') # 文件不存在,創(chuàng)建文件并打開 for info in student: student_txt.write(str(info) + "\n") # 執(zhí)行存儲,添加換行符 student_txt.close() # 關(guān)閉文件 '''查詢學(xué)生信息''' def search(): mark = True student_query = [] 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() for list in student: d = dict(eval(list)) if id is not "": if d['id'] == id: student_query.append(d) elif name is not "": if d['name'] == name: student_query.append(d) show_student(student_query) student_query.clear() inputMark = input("是否繼續(xù)查詢?(y/n):") if inputMark == "y": mark = True else: mark = False else: print("暫未保存數(shù)據(jù)信息...") return '''將保存在列表中的學(xué)生信息顯示出來''' def show_student(studentList): if not studentList: print("無效的數(shù)據(jù)") 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{:^10}\t{:^10}\t{:^10}\t{:10}" 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))) '''刪除學(xué)生信息''' def delete(): mark = True # 標(biāo)記是否循環(huán) while mark: studentId = input("請輸入要刪除的學(xué)生ID:") if studentId is not "": # 判斷要刪除的學(xué)生ID是否存在 if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() 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") # 將一條信息寫入文件 else: ifdel = True # 標(biāo)記已經(jīng)刪除 if ifdel: print("ID為%s的學(xué)生信息已經(jīng)被刪除..." % studentId) else: print("沒有找到ID為%s的學(xué)生信息..." % studentId) else: print("不存在學(xué)生信息") break show() # 顯示全部學(xué)生信息 inputMark = input("是否繼續(xù)刪除?(y/n):") if inputMark == "y": mark = True # 繼續(xù)刪除 else: mark = False # 退出刪除學(xué)生信息操作 '''修改學(xué)生信息''' def modify(): show() # 顯示全部學(xué)生信息 if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() else: return studentid = input("請輸入要修改的學(xué)生ID:") with open(filename, 'w') as wfile: for student in student_old: d = dict(eval(student)) if d['id'] == studentid: 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 student = str(d) # 將字典轉(zhuǎn)為字符串 wfile.write(student + "\n") # 將修改信息寫入到文件 print("修改成功") else: wfile.write(student) # 將未修改的信息寫入文件 mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):") if mark == "y": modify() '''排序''' def sort(): show() if os.path.exists(filename): with open(filename, 'r') as file: student_old = file.readlines() student_new = [] for list in student_old: d = dict(eval(list)) student_new.append(d) else: return ascORdesc = input("請選擇(0升序;1降序)") if ascORdesc == "0": ascORdescBool = False # 標(biāo)記變量,為False時表示升序,為True時表示降序 elif ascORdesc == "1": ascORdescBool = 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é)果 '''統(tǒng)計(jì)學(xué)生總數(shù)''' def total(): if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() if student_old: print("一共有%d名學(xué)生!" % len(student_old)) else: print("還沒有錄入學(xué)生信息") else: print("暫未保存數(shù)據(jù)信息") '''顯示所有學(xué)生信息''' def show(): student_new = [] if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() for list in student_old: student_new.append(eval(list)) if student_new: show_student(student_new) else: print("暫未保存數(shù)據(jù)信息") if __name__ == '__main__': main()
關(guān)于管理系統(tǒng)的更多內(nèi)容請點(diǎn)擊《管理系統(tǒng)專題》進(jìn)行學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python學(xué)生信息管理系統(tǒng)(完整版)
- Python實(shí)現(xiàn)GUI學(xué)生信息管理系統(tǒng)
- python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼
- python實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)(初級版)
- python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼
- python代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)完整實(shí)例
相關(guān)文章
Python全面解析json數(shù)據(jù)并保存為csv文件
這篇文章主要介紹了Python全面解析json數(shù)據(jù)并保存為csv文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Python技法之簡單遞歸下降Parser的實(shí)現(xiàn)方法
遞歸下降解析器可以用來實(shí)現(xiàn)非常復(fù)雜的解析,下面這篇文章主要給大家介紹了關(guān)于Python技法之簡單遞歸下降Parser的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Python反向傳播實(shí)現(xiàn)線性回歸步驟詳細(xì)講解
回歸是監(jiān)督學(xué)習(xí)的一個重要問題,回歸用于預(yù)測輸入變量和輸出變量之間的關(guān)系,特別是當(dāng)輸入變量的值發(fā)生變化時,輸出變量的值也隨之發(fā)生變化?;貧w模型正是表示從輸入變量到輸出變量之間映射的函數(shù)2022-10-10Python使用sqlalchemy模塊連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python使用sqlalchemy模塊連接數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了sqlalchemy模塊的安裝及連接、調(diào)用數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-03-03