import os
import re
filename = "students.txt"
account = {}
account['login'] = str(input("请创建您的账号:"))
account['password'] = str(input("请输入您的密码:"))
print("注册成功,您的账号是:", account['login'].strip())
def main():
ctrl = True
print("*****欢迎登陆学生信息管理系统*****")
login = input("请输入您的账号:")
password = input("请输入您的密码:")
while ctrl:
if login.strip() == account['login'].strip() and password.strip() == account['password'].strip():
menu()
option = input("请选择:")
open_str = int(re.sub("\D", "", option))
if open_str == 0:
print("您已经退出学生信息管理系统!")
ctrl = False
elif open_str == 1:
insert()
elif open_str == 2:
search()
elif open_str == 3:
delete()
elif open_str == 4:
modify()
elif open_str == 5:
sort()
elif open_str == 6:
total()
elif open_str == 7:
show()
else:
print("您输入的账号或密码错误!!!")
break
def menu():
print('='*30)
print('欢迎使用学生信息管理系统1.0')
print('1.录入学生信息')
print('2.查找学生信息')
print('3.删除学生信息')
print('4.修改学生信息')
print('5.学生信息排序')
print('6.统计学生人数')
print('7.显示学生信息')
print('8.退出管理系统')
def save(student):
try:
students_txt = open(filename, 'a', encoding='UTF8')
except Exception as e:
students_txt = open(filename, 'w', encoding='UTF8')
for info in student:
students_txt.write(str(info) + "\n")
students_txt.close()
def insert():
stdentList = []
mark = True
while mark:
id = input("请输入ID(如242560):").strip()
if not id:
break
name = input("请输入名字:").strip()
if not name:
break
try:
chinese = int(input("请输入语文成绩:"))
maths = int(input("请输入数学成绩:"))
english = int(input("请输入英语成绩"))
except:
print("输入无效,不是整数型。。。。。。重新输入!")
continue
stdent = {'id': id, 'name': name, 'chinese': chinese, 'maths': maths, 'english': english}
stdentList.append(stdent)
inputMark = input("是否继续添加? (y/n):")
if inputMark == "y":
mark = True
else:
mark = False
save(stdentList)
print("学生信息录入完毕!!!")
def search():
mark = True
student_query = []
while mark:
id = ""
name = ""
if os.path.exists(filename):
mode = int(input("按ID查输入1,按姓名查输入2:"))
if mode == 1:
id = input("请输入要查找的ID:")
elif mode == 2:
name = input("请输入要查找的名字:")
else:
print("您输入的有误,请重新输入!")
search()
with open(filename, 'r', encoding='UTF8') as file:
student = file.readlines()
for list in student:
d = dict(eval(list))
if id is not None:
if d['id'] == id:
student_query.append(d)
elif name is not None:
if d['name'] == name:
student_query.append(d)
show_student(student_query)
student_query.clear()
inputMark = input("是否继续查询? (y/n):")
if inputMark == "y":
mark = True
else:
mark = False
else:
print("暂未保存数据!\n")
return
def delete():
mark = True
while mark:
studentId = input("请输入要删除的学生ID:")
if studentId is not None:
if os.path.exists(filename):
with open(filename, 'r', encoding='UTF8') as rfile:
student_old = rfile.readlines()
else:
student_old = []
ifdel = False
if student_old:
with open(filename, 'w', encoding='UTF8') 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 的学生信息已经被删除。。。" % studentId)
else:
print("没有找到ID为 %s 的学生信息。。。" % studentId)
else:
print("无学生信息...")
break
show()
inputMark = input("是否继续删除? (y/n) :")
if inputMark == 'y':
mark = True
else:
mark = False
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='UTF8') as rfile:
student_old = rfile.readlines()
else:
return
studentid = input("请输入要修改学生的ID:")
with open(filename, 'w', encoding='UTF8') as wfile:
for student in student_old:
d = dict(eval(student))
if d["id"] == studentid:
print("找到这名学生,可以修改他的信息!")
while True:
try:
d["name"] = input("请输入姓名:")
d["chinese"] = input("请输入语文成绩:")
d["maths"] = input("请输入数学成绩:")
d["english"] = input("请输入英语成绩")
except:
print("您输入的有误,请重新输入!")
else:
break
student = str(d)
wfile.write(student + "\n")
print("修改成功!")
else:
wfile.write(student)
mark = input("是否继续修改其他学生信息? (y/n):")
if mark == "y":
modify()
def sort():
global ascORdescBool
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='UTF8') 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
elif ascORdesc == "1":
ascORdescBool = True
else:
print("您输入有误,请重新输入!")
sort()
mode = input("请选择排序方式(1按英语成绩排序、2按数学成绩排序、3按语文成绩排序、0按总成绩排序):")
if mode == "1":
student_new.sort(key=lambda x: x["englishi"], reverse=ascORdescBool)
elif mode == "2":
student_new.sort(key=lambda x: x["maths"], reverse=ascORdescBool)
elif mode == "3":
student_new.sort(key=lambda x: x["chinese"], reverse=ascORdescBool)
elif mode == "0":
student_new.sort(key=lambda x: x["maths"] + x["english"] + x["chinese"], reverse=ascORdescBool)
else:
print("您输入的有误,请重新输入!")
sort()
show_student(student_new)
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='UTF8') 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', encoding='UTF8') 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 show_student(studentList):
if not studentList:
print("(o@.@o) 无数据信息 (o@.@o) \n")
return
format_title = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"
print(format_title.format("ID", "名字", "英语成绩", "数学成绩", "语文成绩", "总成绩"))
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("maths")),
str(info.get("chinese")),
str(info.get("english") + info.get("maths") + info.get("chinese")).center(12)))