【練手程式】簡單的學生學籍管理系統
阿新 • • 發佈:2018-11-09
這是個簡單的學籍管理系統,將學生資訊放在大字典當中,以學生學號為鍵,以包括學生其他資訊的一個小字典為值,做到了對簡單資訊的增刪改查。
以下為原始碼(因為語言表述不準確,所以未在程式內進行註釋,怕註釋不能盡其義):
def ReadFromFile(path): with open(path,"r") as filer: content = filer.read() return eval(content) def WriteToFile(path,Additional): with open(path,"w") as filew: filew.write(Additional) return True userdic = ReadFromFile("D:\\StudentManagerSystem.txt") print("-" * 30) print("學生學籍管理系統 V2.0") print("輸入以下序號可執行對應功能:") print("1:新增學生學籍資訊。") print("2:刪除學生學籍資訊。") print("3:修改學生學籍資訊。") print("4:查詢學生學籍資訊。") print("5:查詢所有學生學籍資訊。") print("6:安全退出系統。") print("-" * 30) while True: function = int(input("請輸入您需要進行的功能編號:")) if function == 1: print("您選擇了新增學生學籍資訊。") StudentId = input("請輸入學生學號:") for BianHao in userdic.keys(): if StudentId == BianHao: print("您輸入的學生資訊已存在。") break else: continue else: StudentName = input("請輸入學生姓名:") StudentAge = input("請輸入學生年齡:") StudentAddress = input("請輸入學生戶口所在地:") NeiDic = {"姓名":StudentName,"年齡":StudentAge,"住址":StudentAddress} userdic[StudentId] = NeiDic userdic_w = str(userdic) WriteToFile("D:\\StudentManagerSystem.txt",userdic_w) print("新增資訊成功。") elif function == 2: print("您選擇了刪除學生學籍資訊。") StudentId = input("請輸入學生學號:") for BianHao in userdic.keys(): if StudentId == BianHao: del userdic[StudentId] print("刪除學生學籍資訊成功。") break else: print("您輸入的學生學號不存在。") elif function == 3: print("您選擇了修改學生學籍資訊。") StudentId = input("請輸入您要修改的學生編號:") ModifyNumber = int(input("請輸入您要修改的內容(1:修改學號;2:修改姓名;3:修改年齡;4:修改住址;0:退出修改):")) if ModifyNumber == 1: StudentId_new = input("請輸入修改後的學生編號:") for BianHao_new in userdic.keys(): if BianHao_new == StudentId_new: print("您輸入的學生編號已存在。") break else: for BianHao in userdic.keys(): if StudentId == str(BianHao): userdic[StudentId_new] = userdic.pop(StudentId) userdic_w = str(userdic) WriteToFile("D:\\StudentManagerSystem.txt", userdic_w) else: print("您要修改的學生編號不存在。") elif ModifyNumber == 2: StudentName = input("請輸入學生姓名:") userdic[StudentId]["姓名"] = StudentName userdic_w = str(userdic) WriteToFile("D:\\StudentManagerSystem.txt", userdic_w) print("修改學生姓名資訊成功。") elif ModifyNumber == 3: StudentAge = input("請輸入學生年齡:") userdic[StudentId]["年齡"] = StudentAge userdic_w = str(userdic) WriteToFile("D:\\StudentManagerSystem.txt", userdic_w) print("修改學生年齡資訊成功。") elif ModifyNumber == 4: StudentAddress = input("請輸入學生住址:") userdic[StudentId]["住址"] = StudentAddress userdic_w = str(userdic) WriteToFile("D:\\StudentManagerSystem.txt", userdic_w) print("修改學生住址資訊成功。") elif ModifyNumber == 0: pass elif function == 4: print("您選擇了查詢學生學籍資訊:") StudentId = input("請輸入學生學號:") for BianHao in userdic.keys(): if StudentId == BianHao: print("姓名:",userdic[StudentId]["姓名"],";年齡:",userdic[StudentId]["年齡"],";住址:",userdic[StudentId]["住址"]) break else: print("您查詢的學生資訊不存在。") elif function == 5: print("您選擇了查詢全體學生的學籍資訊:") print(" 學號","\t"*3,"姓名","\t"*3,"年齡","\t"*2,"住址") for k,v in userdic.items(): print(" ",k,end="") for w in v.values(): print("\t"*3,w,end="") print("") elif function == 6: Quit = int(input("確認退出本系統?輸入1(是)或者2(否)")) if Quit == 1: print("謝謝您的使用,lyy將竭誠為您服務。") break elif Quit == 2: pass
這是剛學習時候自己寫的,基本沒有查閱資料,改BUG改了一下午,確實有點辛苦,但是感覺敲程式碼的時候會是有點滿足感的成分在裡面,加油。
以下附程式執行結果的兩張圖: