您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么用python實現學生信息管理系統”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
python實現學生信息管理系統,供大家參考,具體內容如下
#!/usr/bin/env python # -*- coding:utf-8 -*- import re import os # 主函數 def main(): ctrl = True while (ctrl): menu() option = input("請選擇:") option_str = re.sub("\D", "", option) if option_str in ['0', '1', '2', '3', '4', '5', '6', '7']: option_int = int(option_str) if option_int == 0: print("您已退出學生信息管理系統!") ctrl = False elif option_int == 1: insert() elif option_int == 2: search() elif option_int == 3: delete() elif option_int == 4: modify() elif option_int == 5: sort() elif option_int == 6: total() elif option_int == 7: show() # 顯示主菜單 def menu(): print(""" -------------------學生信息管理系統--------------------- ======================功能菜單========================= 1 錄入學生信息 2 查找學生信息 3 刪除學生信息 4 修改學生信息 5 排序 6 統計學生總人數 7 顯示所有學生信息 0 退出系統 ====================================================== 說明:通過數字或上下方向鍵選擇菜單 ------------------------------------------------------ """) # 向指定文件寫入指定內容的函數 filename = "students.txt" def save(student): try: students_txt = open(filename, "a") except: students_txt = open(filename, "w") for info in student: students_txt.write(str(info) + "\n") students_txt.close() # 1 錄入學生信息 def insert(): studentList = [] mark = True while mark: id = input("請輸入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("輸入無效,不是整型數值....重新錄入信息") continue # 信息保存到字典 student = {"id": id, "name": name, "english": english, "python": python, "C語言": c} studentList.append(student) inputMark = input("是否繼續添加?(y/n):") if inputMark == "y": mark = True else: mark = False save(studentList) print("學生信息錄入完畢!") # 2 查找學生信息 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("請輸入學生ID:") elif mode == "2": name = input("請輸入學生姓名:") 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("是否繼續查詢?(y/n):") if inputMark == "y": mark = True else: mark = False else: print("暫未保存數據信息...") return def show_student(studentList): if not studentList: print("無數據信息") 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))) # 3 刪除學生信息 def delete(): mark = True while mark: studentId = input("請輸入要刪除的學生ID:") if studentId is not "": if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() else: student_old = [] ifdel = False if student_old: with open(filename, 'w') as wfile: d = {} for list in student_old: d = dict(eval(list)) if d['id'] != studentId: wfile.write(str(d) + "\n") else: ifdel = True if ifdel: print("ID為%s的學生信息已經被刪除..." % studentId) else: print("沒有找到ID為%s的學生信息..." % studentId) else: print("無學生信息...") break show() inputMark = input("是否繼續刪除?(y/n):") if inputMark == "y": mark = True else: mark = False # 4 修改學生信息 def modify(): show() if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() else: return studentid = input("請輸入要修改的學生ID:") with open(filename, "w") as wfile: for student in student_old: d = dict(eval(student)) if d["id"] == studentid: print("找到了這名學生,可以修改他的信息!") 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) wfile.write(student + "\n") print("修改成功!") else: wfile.write(student) mark = input("是否繼續修改其他學生信息?(y/n):") if mark == "y": modify() # 5 排序 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 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": student_new.sort(key=lambda x: x["python"], reverse=ascOrDescBool) elif mode == "3": 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) # 6 統計學生總人數 def total(): if os.path.exists(filename): with open(filename, 'r') as rfile: student_old = rfile.readlines() if student_old: print("一共有%s名學生" % len(student_old)) else: print("還沒有錄入學生信息!") else: print("暫未保存數據信息...") # 7 顯示所有學生信息 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("暫未保存數據信息...") # 0 退出系統 if __name__ == '__main__': main()
安裝pyinstaller打包成可執行exe文件
pip install pyinstaller ... (pydemo) D:\JavaProjects\PythonProject\01學生信息管理系統>pyinstaller --version 4.3 (pydemo) D:\JavaProjects\PythonProject\01學生信息管理系統>pyinstaller -F D:\JavaProjects\PythonProject\01學生信息管理系統\StuManagerSys.py
在下面的路徑即可找到打包后的exe文件
“怎么用python實現學生信息管理系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。