python學生管理系統


import os
import re

#獲取本機用戶名,構建student.txt文件名創建在左面
import getpass
username=getpass.getuser()
print("當前登錄用戶名"+username)
filename="student.txt"
filename="C:\\Users\\"+username+"\\Desktop"+"\\"+filename
print(filename)
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=0
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(): #first page
print(
"""
學生信息管理系統

===============功能菜單===============
|
| 1 錄入學生信息
| 2 查找學生信息
| 3 刪除學生信息
| 4 修改學生信息
| 5 排序
| 6 統計學生總人數
| 7 顯示所有學生信息
| 0 退出管理系統
=======================================
說明:通過數字或↑↓方向鍵選擇菜單
"""
)
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" or inputmark=="y":
mark=1
else:
mark=0
print(studentlist)
save(studentlist)
print("學生信息錄入完畢")




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" or inputmark=="Y":
mark=True
else:
mark=False
else:
print("暫未保存數據信息")
return
def delete():
mark=True
while mark:
studentid=input("請輸入你要刪除的學生id,如果輸入為空則退出刪除")
if studentid=="":
return 1
if studentid is not "": #此處判斷輸入是否為空,如果輸入有效執行讀取信息
if os.path.exists(filename):
with open(filename,"r") as rfile:
student_old=rfile.readlines()
else: #如過為獲取到文件內容,則返回空列表。stuent_ol=[]=false
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")
#此處相當於讀取了所有學生信息,除了id為輸入要刪除的id以外,其他學生的信息全部重新寫入
else: #如果 d['id']!=studentid ,那么ifdel=true,沒有寫入證明刪除成功
ifdel=True
if ifdel:
print("id號為%s的學生信息已經刪除"%studentid)
else:
print("沒有找到id為%s的學生信息"%studentid)
else:
print("無學生信息")
break
show()
inputmark=input("是否繼續刪除(Y/N):")
if inputmark=="y" or inputmark=='Y':
mark=True
else:
mark=False
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("找到該學生id,可以修改信息")
while 1:
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" or mark == "Y":
modify()
else:
return 1

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":
ascbool=False
if ascordesc=="1":
ascbool=True
else:
print("你的輸入有誤,請重新輸入")
sort()
mode=input("""請選擇排序方式(1 按英語成績排序 2 按python成績排序 3 按c語言成績排序 0 按總成績排序 )""")
if mode=="1":
student_new.sort(key=lambda x:x["english"],reverse=ascbool)
elif mode=="2":
student_new.sort(key=lambda x:x["python"],reverse=ascbool)
elif mode=="3":
student_new.sort(key=lambda x:x["c"],reverse=ascbool)
elif mode=="0":
student_new.sort(key=lambda x: x["english"]+x["python"]+x["c"], reverse=ascbool)
else:
print("輸入有誤,請重新輸入")
sort()
show_student(student_new)


def total():
if os.path.exists(filename):
with open(filename,"r") as rfile:
student_old=rfile.readlines()
if student_old:
print("一共有%d名學生"%len(student_old))
else:
print("還沒有錄入學生信息")
else:
print("暫時未保存數據信息")
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("暫未找到數據信息")
def save(student):
try:
students_txt=open(filename,"a")
except Exception as e:
students_txt=open(filename,"w")
for info in student:
students_txt.write(str(info)+"\n")
students_txt.close()
def show_student(studentList):
if not studentList:
print("!!--無數據信息--!!\n")
return
format_title="{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"
print(format_title.format("id","姓名","英語成績","python成績","語言成績","總成績"))
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("c")+info.get("python")).center(12)
))
main()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM