1. 程式人生 > 程式設計 >python實現簡單學生資訊管理系統

python實現簡單學生資訊管理系統

python簡單的學生資訊管理系統-檔案版,供大家參考,具體內容如下

功能如下

主函式部分
增加學生資訊
修改學生資訊
刪除學生資訊
查詢學生
顯示所有學生的資訊
將資料錄入檔案
讀取檔案資料

學習檔案模組後,將之前做的學生資訊管理系統新增檔案模組。

功能如下

1、新增學生資訊;
2、修改學生資訊;
3、刪除學生資訊;
4、查詢學生資訊;
5、顯示學生資訊;
6、將資料錄入檔案;
7、讀取檔案資料;
8、退出系統。

python實現簡單學生資訊管理系統

主函式部分

這裡定義一個列表L,用來儲存學生資訊。

python實現簡單學生資訊管理系統

增加學生資訊

將學生資訊儲存為字典新增到列表裡。

def add():
 dict1 = {}
 sName = input("請輸入學生姓名:")
 sAge = eval(input("請輸入學生年齡:"))
 sNumber = eval(input("請輸入學生學號:"))
 tele_num = eval(input("請輸入手機號碼:"))
 dict1["name"] = sName
 dict1["age"] = sAge
 dict1["sNumber"] = sNumber
 dict1["tele_num"] = tele_num
 L.append(dict1)
 print("增加成功")
 input("按任意鍵返回選單")

python實現簡單學生資訊管理系統

修改學生資訊

這裡以學號為索引值,如果學號不在學生庫裡則提示無此學生。

def modify():
 num = eval(input("請輸入學生學號:"))
 index1 = -1
 for i,dict in enumerate(L):
 if dict.get("sNumber") == num:
 index1 = i
 break
 if index1 != -1:
 L[index1]['name'] = input("請輸入新的姓名:")
 L[index1]['age'] = eval(input("請輸入新的年齡:"))
 L[index1]['sNumber'] = eval(input("請輸入新的學號:"))
 L[index1]['tele_num'] = eval(input("請輸入新的手機號:"))
 print("修改成功")
 else:
 print("無此學生")
 input("按任意鍵返回選單")

刪除學生資訊

刪除學生也是以學號為索引值,如果學號不在學生庫裡則提示無此學生。

def delete():
 num = eval(input("請輸入要刪除學生的學號:"))
 index1 = -1
 for i,dict in enumerate(L):
 if dict.get("sNumber") == num:
 index1 = i
 break
 if index1 != -1:
 del L[index1]
 print("刪除成功")
 else:
 print("無此學生")
 input("按任意鍵返回選單")

查詢學生

查詢成功此顯示學生資訊,否則提示無此學生。

def search():
 num = eval(input("請輸入要查詢學生的學號:"))
 index1 = -1
 for i,dict in enumerate(L):
 if dict.get("sNumber") == num:
 index1 = i
 break
 if index1 != -1:
 print("姓名:%s 年齡:%d 學號:%d 手機號碼:%d" % (L[index1]["name"],L[index1]["age"],\
 L[index1]["sNumber"],L[index1]["tele_num"]))
 else:
 print("無此學生")
 input("按任意鍵返回選單")

顯示所有學生的資訊

def prin():
 if len(L) == 0:
 print("無成員")
 else:
 for dict1 in L:
 print("姓名:%s 年齡:%d 學號:%d 手機號碼:%d"%(dict1["name"],dict1["age"],\
 dict1["sNumber"],dict1["tele_num"]))
 input("按任意鍵返回選單")

將資料錄入檔案

因為每個學生的資訊是字典型別,所以錄入之前先轉化為字串。

def write_file():
 file = open("student_list.data",'w',encoding='utf-8')
 for i in L:
 file.write(str(i) + '\n')
 file.close()
 input("錄入成功,按任意鍵返回選單!")

讀取檔案資料

讀取到的資訊是字串,可以使用eval()函式將資訊變為原來的字典型別,再新增到列表裡。

def read_file():
 try:
 file = open("student_list.data",'r',encoding='utf-8')
 content = file.readlines()
 for i in content:
 L.append(eval(i))
 file.close()
 input("讀取完成,按任意鍵返回選單!")
 except:
 print("檔案不存在")

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。