使用Python實現 學生學籍管理系統
阿新 • • 發佈:2020-01-09
大家好,今天跟大家分享一個用Python實現的學生學籍管理系統:
該程式碼主體由五個函式組成:
1.add_stu() 新增
2.del_stu() 刪除
3.print_stu()列印
4.exit_stu() 退出
5.system() 主函式
1.add_stu()
此段函式作用:把輸入的值存入字典newstu中,並將字典存入列表stu中
def add_stu(): newstu = { 'num':int(input("請輸入學號:"'')),'name':input("請輸入姓名:"''),'sex':input("請輸入性別:"'') } stu.append(newstu) #stu是定義的列表,可在下面的完整程式碼中查詢 return system()
2.del_stu()
此段函式作用:根據輸入學號,查詢列表中的字典鍵值是否存入,若存入,將該字典從列表中刪除
def del_stu(): delstus = int(input("請輸入要刪除的學生學號:")) k = 0 #迴圈遞增變數,用來判斷要刪除的字典在列表中的位置 for temp in stu: k+=1 if delstus in range(temp['num'],temp['num']-1,-1): #這裡的temp變數相當於列表中的字典,通過查詢鍵值是否相匹配,並用到range函式(start,stop,step) print("該學號已找到") break del stu[( k - 1 )]#刪除列表中的字典 print("刪除成功") return system()
3.print_stu()
此段函式作用:把列表中的字典以值的形式遍歷出來
def print_stu(): i = 1 j = 0 k = 1 print("=================================") print("學生資訊如下:") print("=================================") print("序號\t學號\t姓名\t性別") for s in stu:#遍歷列表 for cla in s.values(): #字典中的按值索引 if j%3 == 0:#第一個if用來實現:序號遞增並換行 print("\n") print(k,end = '.\t') k+=1 print(cla,end = '\t') if i%3 == 0:#第二個if用來實現:每輸出一個字典就換行 print("\n") i+=1 j+=1 print("\n") return system()
4.exit_stu()
此段函式作用:人機互動,退出
def exit_stu(): x = input("是否退出?( YES or NO ) : ") if x == 'YES': print("***Repl Closed*** ") else: return system()
5.system_stu()
主函式
def system(): print("=================================") print("學生管理系統v1.0") print("1.新增學生資訊") print("2.刪除學生資訊") print("3.顯示所有的學生資訊") print("0.退出系統") print("=================================") x = int(input("請輸入功能對應的數字: ")) if( x == 1): add_stu() elif( x == 2): del_stu() elif( x == 3): print_stu() elif( x == 0): exit_stu() else: return system()
完整程式碼
stu = [] def add_stu(): newstu = { 'num':int(input("請輸入學號:"'')),'sex':input("請輸入性別:"'') } stu.append(newstu) return system() def del_stu(): delstus = int(input("請輸入要刪除的學生學號:")) k = 0 for temp in stu: k+=1 if delstus in range(temp['num'],-1): print("該學號已找到") break del stu[( k - 1 )] print("刪除成功") return system() def print_stu(): i = 1 j = 0 k = 1 print("=================================") print("學生資訊如下:") print("=================================") print("序號\t學號\t姓名\t性別") for s in stu: for cla in s.values(): if j%3 == 0: print("\n") print(k,end = '\t') if i%3 == 0: print("\n") i+=1 j+=1 print("\n") return system() def exit_stu(): x = input("是否退出?( YES or NO ) : ") if x == 'YES': print("***Repl Closed*** ") else: return system() def system(): print("=================================") print("學生管理系統v1.0") print("1.新增學生資訊") print("2.刪除學生資訊") print("3.顯示所有的學生資訊") print("0.退出系統") print("=================================") x = int(input("請輸入功能對應的數字: ")) if( x == 1): add_stu() elif( x == 2): del_stu() elif( x == 3): print_stu() elif( x == 0): exit_stu() else: return system() system()
難點
1.在列表中,按學號索引字典
2. 列印列表時的換行和序號
總結
以上所述是小編給大家介紹的使用Python實現 學生學籍管理系統,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!