python實現(xiàn)簡易學(xué)生信息管理系統(tǒng)
本文實例為大家分享了python實現(xiàn)學(xué)生信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
簡易學(xué)生信息管理系統(tǒng)主要功能有
1 錄入學(xué)生信息
2 查找學(xué)生信息
3 刪除學(xué)生信息
4 修改學(xué)生信息
5 排序
6 統(tǒng)計學(xué)生總?cè)藬?shù)
7 顯示所有學(xué)生信息
0 退出系統(tǒng)
系統(tǒng)運行效果

主菜單的代碼方法:
# Author: dry
# 開發(fā)時間:2019/9/11
# 開發(fā)工具:PyCharm
import re # 導(dǎo)入正則表達式模塊
import os # 導(dǎo)入操作系統(tǒng)模塊
filename = "student.txt" # 學(xué)生信息保存文件
def menu():
# 輸出菜單
print('''
---------------學(xué)生信息管理系統(tǒng)------------
==================功能菜單================
1 錄入學(xué)生信息
2 查找學(xué)生信息
3 刪除學(xué)生信息
4 修改學(xué)生信息
5 排序
6 統(tǒng)計學(xué)生總?cè)藬?shù)
7 顯示所有學(xué)生信息
0 退出系統(tǒng)
=======================================
說明:通過數(shù)字選擇菜單
=======================================
''')
系統(tǒng)主方法:
def main():
ctrl = True # 標記是否退出系統(tǒng)
while (ctrl):
menu() # 顯示菜單
option = input("請選擇:") # 選擇菜單項
option_str = re.sub("\D", "", option) # 提取數(shù)字
if option_str in ['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)計學(xué)生總數(shù)
total()
elif option_int == 7: # 顯示所有學(xué)生信息
show()
錄入學(xué)生信息方法:
'''錄入學(xué)生信息'''
def insert():
studentList = [] # 保存學(xué)生信息的列表
mark = True # 是否繼續(xù)添加
while mark:
id = input("請輸入學(xué)生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("輸入無效,不是整型數(shù)值,請重新輸入信息")
continue
# 將輸入的學(xué)生信息保存到字典
student = {"id": id, "name": name, "english": english, "python": python, "c": c}
studentList.append(student) # 將學(xué)生字典添加到列表中
inputList = input("是否繼續(xù)添加?(y/n):")
if inputList == 'y': # 繼續(xù)添加
mark = True
else:
mark = False
save(studentList) # 將學(xué)生信息保存到文件
print("學(xué)生信息錄入完畢!!!")
保存學(xué)生信息方法:
'''將學(xué)生信息保存到文件''' def save(student): try: student_txt = open(filename, 'a') # 以追加模式打開 except Exception as e: student_txt = open(filename, 'w') # 文件不存在,創(chuàng)建文件并打開 for info in student: student_txt.write(str(info) + "\n") # 執(zhí)行存儲,添加換行符 student_txt.close() # 關(guān)閉文件
查詢學(xué)生信息方法:
'''查詢學(xué)生信息'''
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("請輸入學(xué)生ID:")
elif mode == "2":
name = input("請輸入學(xué)生姓名:")
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("是否繼續(xù)查詢?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
else:
print("暫未保存數(shù)據(jù)信息...")
return
顯示學(xué)生信息方法
'''將保存在列表中的學(xué)生信息顯示出來'''
def show_student(studentList):
if not studentList:
print("無效的數(shù)據(jù)")
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{:^10}\t{:^10}\t{:^10}\t{:10}"
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("python") + info.get("c")).center(12)))
刪除學(xué)生信息方法:
'''刪除學(xué)生信息'''
def delete():
mark = True # 標記是否循環(huán)
while mark:
studentId = input("請輸入要刪除的學(xué)生ID:")
if studentId is not "": # 判斷要刪除的學(xué)生ID是否存在
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
student_old = []
ifdel = False # 標記是否刪除
if student_old: # 如果存在學(xué)生信息
with open(filename, 'w') as wfile:
d = {} # 定義空字典
for list in student_old:
d = dict(eval(list)) # 字符串轉(zhuǎn)字典
if d['id'] != studentId:
wfile.write(str(d) + "\n") # 將一條信息寫入文件
else:
ifdel = True # 標記已經(jīng)刪除
if ifdel:
print("ID為%s的學(xué)生信息已經(jīng)被刪除..." % studentId)
else:
print("沒有找到ID為%s的學(xué)生信息..." % studentId)
else:
print("不存在學(xué)生信息")
break
show() # 顯示全部學(xué)生信息
inputMark = input("是否繼續(xù)刪除?(y/n):")
if inputMark == "y":
mark = True # 繼續(xù)刪除
else:
mark = False # 退出刪除學(xué)生信息操作
修改學(xué)生信息方法:
'''修改學(xué)生信息'''
def modify():
show() # 顯示全部學(xué)生信息
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
return
studentid = input("請輸入要修改的學(xué)生ID:")
with open(filename, 'w') as wfile:
for student in student_old:
d = dict(eval(student))
if d['id'] == studentid:
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
student = str(d) # 將字典轉(zhuǎn)為字符串
wfile.write(student + "\n") # 將修改信息寫入到文件
print("修改成功")
else:
wfile.write(student) # 將未修改的信息寫入文件
mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):")
if mark == "y":
modify()
學(xué)生信息排序方法:
'''排序'''
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":
ascORdescBool = False # 標記變量,為False時表示升序,為True時表示降序
elif ascORdesc == "1":
ascORdescBool = 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é)果
統(tǒng)計學(xué)生總數(shù)方法:
'''統(tǒng)計學(xué)生總數(shù)'''
def total():
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
if student_old:
print("一共有%d名學(xué)生!" % len(student_old))
else:
print("還沒有錄入學(xué)生信息")
else:
print("暫未保存數(shù)據(jù)信息")
顯示所有學(xué)生信息方法:
'''顯示所有學(xué)生信息'''
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("暫未保存數(shù)據(jù)信息")
開始函數(shù):
if __name__ == '__main__': main()
完整代碼如下:
# Author: dry
# 開發(fā)時間:2019/9/11
# 開發(fā)工具:PyCharm
import re # 導(dǎo)入正則表達式模塊
import os # 導(dǎo)入操作系統(tǒng)模塊
filename = "student.txt" # 學(xué)生信息保存文件
def menu():
# 輸出菜單
print('''
---------------學(xué)生信息管理系統(tǒng)------------
==================功能菜單================
1 錄入學(xué)生信息
2 查找學(xué)生信息
3 刪除學(xué)生信息
4 修改學(xué)生信息
5 排序
6 統(tǒng)計學(xué)生總?cè)藬?shù)
7 顯示所有學(xué)生信息
0 退出系統(tǒng)
=======================================
說明:通過數(shù)字選擇菜單
=======================================
''')
def main():
ctrl = True # 標記是否退出系統(tǒng)
while (ctrl):
menu() # 顯示菜單
option = input("請選擇:") # 選擇菜單項
option_str = re.sub("\D", "", option) # 提取數(shù)字
if option_str in ['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)計學(xué)生總數(shù)
total()
elif option_int == 7: # 顯示所有學(xué)生信息
show()
'''錄入學(xué)生信息'''
def insert():
studentList = [] # 保存學(xué)生信息的列表
mark = True # 是否繼續(xù)添加
while mark:
id = input("請輸入學(xué)生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("輸入無效,不是整形數(shù)值,請重新輸入信息")
continue
# 將輸入的學(xué)生信息保存到字典
student = {"id": id, "name": name, "english": english, "python": python, "c": c}
studentList.append(student) # 將學(xué)生字典添加到列表中
inputList = input("是否繼續(xù)添加?(y/n):")
if inputList == 'y': # 繼續(xù)添加
mark = True
else:
mark = False
save(studentList) # 將學(xué)生信息保存到文件
print("學(xué)生信息錄入完畢!!!")
'''將學(xué)生信息保存到文件'''
def save(student):
try:
student_txt = open(filename, 'a') # 以追加模式打開
except Exception as e:
student_txt = open(filename, 'w') # 文件不存在,創(chuàng)建文件并打開
for info in student:
student_txt.write(str(info) + "\n") # 執(zhí)行存儲,添加換行符
student_txt.close() # 關(guān)閉文件
'''查詢學(xué)生信息'''
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("請輸入學(xué)生ID:")
elif mode == "2":
name = input("請輸入學(xué)生姓名:")
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("是否繼續(xù)查詢?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
else:
print("暫未保存數(shù)據(jù)信息...")
return
'''將保存在列表中的學(xué)生信息顯示出來'''
def show_student(studentList):
if not studentList:
print("無效的數(shù)據(jù)")
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{:^10}\t{:^10}\t{:^10}\t{:10}"
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("python") + info.get("c")).center(12)))
'''刪除學(xué)生信息'''
def delete():
mark = True # 標記是否循環(huán)
while mark:
studentId = input("請輸入要刪除的學(xué)生ID:")
if studentId is not "": # 判斷要刪除的學(xué)生ID是否存在
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
student_old = []
ifdel = False # 標記是否刪除
if student_old: # 如果存在學(xué)生信息
with open(filename, 'w') as wfile:
d = {} # 定義空字典
for list in student_old:
d = dict(eval(list)) # 字符串轉(zhuǎn)字典
if d['id'] != studentId:
wfile.write(str(d) + "\n") # 將一條信息寫入文件
else:
ifdel = True # 標記已經(jīng)刪除
if ifdel:
print("ID為%s的學(xué)生信息已經(jīng)被刪除..." % studentId)
else:
print("沒有找到ID為%s的學(xué)生信息..." % studentId)
else:
print("不存在學(xué)生信息")
break
show() # 顯示全部學(xué)生信息
inputMark = input("是否繼續(xù)刪除?(y/n):")
if inputMark == "y":
mark = True # 繼續(xù)刪除
else:
mark = False # 退出刪除學(xué)生信息操作
'''修改學(xué)生信息'''
def modify():
show() # 顯示全部學(xué)生信息
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
return
studentid = input("請輸入要修改的學(xué)生ID:")
with open(filename, 'w') as wfile:
for student in student_old:
d = dict(eval(student))
if d['id'] == studentid:
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
student = str(d) # 將字典轉(zhuǎn)為字符串
wfile.write(student + "\n") # 將修改信息寫入到文件
print("修改成功")
else:
wfile.write(student) # 將未修改的信息寫入文件
mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):")
if mark == "y":
modify()
'''排序'''
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":
ascORdescBool = False # 標記變量,為False時表示升序,為True時表示降序
elif ascORdesc == "1":
ascORdescBool = 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é)果
'''統(tǒng)計學(xué)生總數(shù)'''
def total():
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
if student_old:
print("一共有%d名學(xué)生!" % len(student_old))
else:
print("還沒有錄入學(xué)生信息")
else:
print("暫未保存數(shù)據(jù)信息")
'''顯示所有學(xué)生信息'''
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("暫未保存數(shù)據(jù)信息")
if __name__ == '__main__':
main()
關(guān)于管理系統(tǒng)的更多內(nèi)容請點擊《管理系統(tǒng)專題》進行學(xué)習
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- python學(xué)生信息管理系統(tǒng)(完整版)
- Python實現(xiàn)GUI學(xué)生信息管理系統(tǒng)
- python實現(xiàn)學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)實現(xiàn)代碼
- python實現(xiàn)簡單學(xué)生信息管理系統(tǒng)
- python學(xué)生信息管理系統(tǒng)(初級版)
- python學(xué)生信息管理系統(tǒng)實現(xiàn)代碼
- python代碼實現(xiàn)學(xué)生信息管理系統(tǒng)
- Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)完整實例
相關(guān)文章
python導(dǎo)入csv文件出現(xiàn)SyntaxError問題分析
這篇文章主要介紹了python導(dǎo)入csv文件出現(xiàn)SyntaxError問題分析,同時涉及python導(dǎo)入csv文件的三種方法,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
Python利用Selenium實現(xiàn)彈出框的處理
經(jīng)常出現(xiàn)在網(wǎng)頁上的基于JavaScript實現(xiàn)的彈出框有三種,分別是?alert、confirm、prompt?。本文主要是學(xué)習如何利用selenium處理這三種彈出框,感興趣的可以了解一下2022-06-06
python數(shù)據(jù)類型的詳細分析(附示例代碼)
這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)類型分析的相關(guān)資料,python里可以通過type()函數(shù)來查看數(shù)據(jù)類型,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09
pandas中apply和transform方法的性能比較及區(qū)別介紹
這篇文章主要介紹了pandas中apply和transform方法的性能比較,在文中給大家講解了apply() 與transform()的相同點與不同點,需要的朋友可以參考下2018-10-10
Django 開發(fā)環(huán)境與生產(chǎn)環(huán)境的區(qū)分詳解
這篇文章主要介紹了Django 開發(fā)環(huán)境與生產(chǎn)環(huán)境的區(qū)分詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-07-07
Python基于OpenCV庫Adaboost實現(xiàn)人臉識別功能詳解
這篇文章主要介紹了Python基于OpenCV庫Adaboost實現(xiàn)人臉識別功能,結(jié)合實例形式分析了Python下載與安裝OpenCV庫及相關(guān)人臉識別操作實現(xiàn)技巧,需要的朋友可以參考下2018-08-08

