import re,os
filename ="students.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_strin ['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()
'''1 錄入學(xué)生信息'''
def insert():
stdentList = []# 保存學(xué)生信息的列表
? ? mark =True? # 是否繼續(xù)添加
? ? while mark:
id =input("請輸入ID(如 1001):")
if not id:# ID為空岂却,跳出循環(huán)
? ? ? ? ? ? break
? ? ? ? name =input("請輸入名字:")
if not name:# 名字為空硫狞,跳出循環(huán)
? ? ? ? ? ? break
? ? ? ? try:
english =int(input("請輸入英語成績:"))
python =int(input("請輸入Python成績:"))
c =int(input("請輸入C語言成績:"))
except:
print("輸入無效,不是整型數(shù)值....重新錄入信息")
continue
? ? ? ? stdent = {"id": id,"name": name,"english": english,"python": python,"c": c}# 將輸入的學(xué)生信息保存到字典
? ? ? ? stdentList.append(stdent)# 將學(xué)生字典添加到列表中
? ? ? ? inputMark =input("是否繼續(xù)添加九串?(y/n):")
if inputMark =="y":# 繼續(xù)添加
? ? ? ? ? ? mark =True
? ? ? ? else:# 不繼續(xù)添加
? ? ? ? ? ? mark =False
? ? save(stdentList)# 將學(xué)生信息保存到文件
? ? print("學(xué)生信息錄入完畢7摺A渍恕!")
# 將學(xué)生信息保存到文件
def save(student):
try:
students_txt =open(filename,"a")# 以追加模式打開
? ? except Exception as e:
students_txt =open(filename,"w")# 文件不存在贾虽,創(chuàng)建文件并打開
? ? for infoin student:
students_txt.write(str(info) +"\n")# 按行存儲逃糟,添加換行符
? ? students_txt.close()# 關(guān)閉文件
'''2 查找學(xué)生成績信息'''
def search():
mark =True
? ? student_query = []# 保存查詢結(jié)果的學(xué)生列表
? ? 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()# 讀取全部內(nèi)容
? ? ? ? ? ? ? ? for listin student:
d =dict(eval(list))# 字符串轉(zhuǎn)字典
? ? ? ? ? ? ? ? ? ? if idis not "":# 判斷是否按ID查
? ? ? ? ? ? ? ? ? ? ? ? if d['id'] == id:
student_query.append(d)# 將找到的學(xué)生信息保存到列表中
? ? ? ? ? ? ? ? ? ? elif nameis not "":# 判斷是否按姓名查
? ? ? ? ? ? ? ? ? ? ? ? if d['name'] == name:
student_query.append(d)# 將找到的學(xué)生信息保存到列表中
? ? ? ? ? ? ? ? show_student(student_query)# 顯示查詢結(jié)果
? ? ? ? ? ? ? ? student_query.clear()# 清空列表
? ? ? ? ? ? ? ? inputMark =input("是否繼續(xù)查詢?(y/n):")
if inputMark =="y":
mark =True
? ? ? ? ? ? ? ? else:
mark =False
? ? ? ? else:
print("暫未保存數(shù)據(jù)信息...")
return
'''3 刪除學(xué)生成績信息'''
def delete():
mark =True? # 標(biāo)記是否循環(huán)
? ? while mark:
studentId =input("請輸入要?jiǎng)h除的學(xué)生ID:")
if studentIdis not "":# 判斷要?jiǎng)h除的學(xué)生是否存在
? ? ? ? ? ? if os.path.exists(filename):# 判斷文件是否存在
? ? ? ? ? ? ? ? with open(filename,'r')as rfile:# 打開文件
? ? ? ? ? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容
? ? ? ? ? ? else:
student_old = []
ifdel =False? # 標(biāo)記是否刪除
? ? ? ? ? ? if student_old:# 如果存在學(xué)生信息
? ? ? ? ? ? ? ? with open(filename,'w')as wfile:# 以寫方式打開文件
? ? ? ? ? ? ? ? ? ? d = {}# 定義空字典
? ? ? ? ? ? ? ? ? ? for listin student_old:
d =dict(eval(list))# 字符串轉(zhuǎn)字典
? ? ? ? ? ? ? ? ? ? ? ? if d['id'] != studentId:
wfile.write(str(d) +"\n")# 將一條學(xué)生信息寫入文件
? ? ? ? ? ? ? ? ? ? ? ? else:
ifdel =True? # 標(biāo)記已經(jīng)刪除
? ? ? ? ? ? ? ? ? ? if ifdel:
print("ID為 %s 的學(xué)生信息已經(jīng)被刪除..." % studentId)
else:
print("沒有找到ID為 %s 的學(xué)生信息..." % studentId)
else:# 不存在學(xué)生信息
? ? ? ? ? ? ? ? print("無學(xué)生信息...")
break? # 退出循環(huán)
? ? ? ? ? ? show()# 顯示全部學(xué)生信息
? ? ? ? ? ? inputMark =input("是否繼續(xù)刪除地粪?(y/n):")
if inputMark =="y":
mark =True? # 繼續(xù)刪除
? ? ? ? ? ? else:
mark =False? # 退出刪除學(xué)生信息功能
'''4 修改學(xué)生成績信息'''
def modify():
show()# 顯示全部學(xué)生信息
? ? if os.path.exists(filename):# 判斷文件是否存在
? ? ? ? with open(filename,'r')as rfile:# 打開文件
? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容
? ? else:
return
? ? studentid =input("請輸入要修改的學(xué)生ID:")
with open(filename,"w")as wfile:# 以寫模式打開文件
? ? ? ? for studentin student_old:
d =dict(eval(student))# 字符串轉(zhuǎn)字典
? ? ? ? ? ? if d["id"] == studentid:# 是否為要修改的學(xué)生
? ? ? ? ? ? ? ? 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? # 跳出循環(huán)
? ? ? ? ? ? ? ? student =str(d)# 將字典轉(zhuǎn)換為字符串
? ? ? ? ? ? ? ? wfile.write(student +"\n")# 將修改的信息寫入到文件
? ? ? ? ? ? ? ? print("修改成功!")
else:
wfile.write(student)# 將未修改的信息寫入到文件
? ? mark =input("是否繼續(xù)修改其他學(xué)生信息质礼?(y/n):")
if mark =="y":
modify()# 重新執(zhí)行修改操作
'''5 排序'''
def sort():
show()# 顯示全部學(xué)生信息
? ? if os.path.exists(filename):# 判斷文件是否存在
? ? ? ? with open(filename,'r')as file:# 打開文件
? ? ? ? ? ? student_old = file.readlines()# 讀取全部內(nèi)容
? ? ? ? ? ? student_new = []
for listin student_old:
d =dict(eval(list))# 字符串轉(zhuǎn)字典
? ? ? ? ? ? student_new.append(d)# 將轉(zhuǎn)換后的字典添加到列表中
? ? else:
return
? ? ascORdesc =input("請選擇(0升序旺聚;1降序):")
if ascORdesc =="0":# 按升序排序
? ? ? ? ascORdescBool =False? ? ? ? ? # 標(biāo)記變量,為False表示升序排序
? ? elif ascORdesc =="1":# 按降序排序
? ? ? ? ascORdescBool =True? ? ? ? ? # 標(biāo)記變量眶蕉,為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é)果
''' 6 統(tǒng)計(jì)學(xué)生總數(shù)'''
def total():
if os.path.exists(filename):# 判斷文件是否存在
? ? ? ? with open(filename,'r')as rfile:# 打開文件
? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容
? ? ? ? ? ? if student_old:
print("一共有 %d 名學(xué)生!" %len(student_old))
else:
print("還沒有錄入學(xué)生信息谐丢!")
else:
print("暫未保存數(shù)據(jù)信息...")
''' 7 顯示所有學(xué)生信息 '''
def show():
student_new = []
if os.path.exists(filename):# 判斷文件是否存在
? ? ? ? with open(filename,'r')as rfile:# 打開文件
? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容
? ? ? ? for listin student_old:
student_new.append(eval(list))# 將找到的學(xué)生信息保存到列表中
? ? ? ? if student_new:
show_student(student_new)
else:
print("暫未保存數(shù)據(jù)信息...")
# 將保存在列表中的學(xué)生信息顯示出來
def show_student(studentList):
if not studentList:
print("(o@.@o) 無數(shù)據(jù)信息 (o@.@o) \n")
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 infoin 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)))
if __name__ =="__main__":
main()
--------------
微信:如火如荼 ? ?歡迎一起探討Python