#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import os
# 主函數(shù)
def main():
ctrl = True
while (ctrl):
menu()
option = input("請(qǐng)選擇:")
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("您已退出學(xué)生信息管理系統(tǒng)!")
ctrl = False
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():
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ù)字或上下方向鍵選擇菜單
------------------------------------------------------
""")
# 向指定文件寫入指定內(nèi)容的函數(shù)
filename = "students.txt"
def save(student):
try:
students_txt = open(filename, "a")
except:
students_txt = open(filename, "w")
for info in student:
students_txt.write(str(info) + "\n")
students_txt.close()
# 1 錄入學(xué)生信息
def insert():
studentList = []
mark = True
while mark:
id = input("請(qǐng)輸入ID(如1001):")
if not id:
break
name = input("請(qǐng)輸入名字:")
if not name:
break
try:
english = int(input("請(qǐng)輸入英語成績(jī):"))
python = int(input("請(qǐng)輸入Python成績(jī):"))
c = int(input("請(qǐng)輸入C語言成績(jī):"))
except:
print("輸入無效,不是整型數(shù)值....重新錄入信息")
continue
# 信息保存到字典
student = {"id": id, "name": name, "english": english, "python": python, "C語言": c}
studentList.append(student)
inputMark = input("是否繼續(xù)添加?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
save(studentList)
print("學(xué)生信息錄入完畢!")
# 2 查找學(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("請(qǐng)輸入學(xué)生ID:")
elif mode == "2":
name = input("請(qǐng)輸入學(xué)生姓名:")
else:
print("您的輸入有誤,請(qǐng)重新輸入!")
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
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", "名字", "英語成績(jī)", "Python成績(jī)", "C語言成績(jī)", "總成績(jī)"))
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("python")+info.get("C語言")).center(12)))
# 3 刪除學(xué)生信息
def delete():
mark = True
while mark:
studentId = input("請(qǐng)輸入要?jiǎng)h除的學(xué)生ID:")
if studentId is not "":
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
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")
else:
ifdel = True
if ifdel:
print("ID為%s的學(xué)生信息已經(jīng)被刪除..." % studentId)
else:
print("沒有找到ID為%s的學(xué)生信息..." % studentId)
else:
print("無學(xué)生信息...")
break
show()
inputMark = input("是否繼續(xù)刪除?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
# 4 修改學(xué)生信息
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
else:
return
studentid = input("請(qǐng)輸入要修改的學(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("請(qǐng)輸入姓名:")
d["english"] = int(input("請(qǐng)輸入英語成績(jī):"))
d["python"] = int(input("請(qǐng)輸入Python成績(jī):"))
d["C語言"] = int(input("請(qǐng)輸入C語言成績(jī):"))
except:
print("輸入信息有誤,重新輸入")
else:
break
student = str(d)
wfile.write(student + "\n")
print("修改成功!")
else:
wfile.write(student)
mark = input("是否繼續(xù)修改其他學(xué)生信息?(y/n):")
if mark == "y":
modify()
# 5 排序
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("請(qǐng)選擇(0升序;1降序):")
if ascOrDesc == "0":
ascOrDescBool = False
elif ascOrDesc == "1":
ascOrDescBool = True
else:
print("您的輸入有誤,請(qǐng)重新輸入!")
sort()
mode = input("請(qǐng)選擇排序方式(1按英語排序;2按Python排序;3按C語言排序;0按總成績(jī)排序)")
if mode == "1":
student_new.sort(key=lambda x: x["english"], reverse=ascOrDescBool)
elif mode == "2":
student_new.sort(key=lambda x: x["python"], reverse=ascOrDescBool)
elif mode == "3":
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("您的輸入有誤,請(qǐng)重新輸入!")
sort()
show_student(student_new)
# 6 統(tǒng)計(jì)學(xué)生總?cè)藬?shù)
def total():
if os.path.exists(filename):
with open(filename, 'r') as rfile:
student_old = rfile.readlines()
if student_old:
print("一共有%s名學(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()
for list in student_old:
student_new.append(eval(list))
if student_new:
show_student(student_new)
else:
print("暫未保存數(shù)據(jù)信息...")
# 0 退出系統(tǒng)
if __name__ == '__main__':
main()