学生管理系统应具备的功能
- 添加学生及成绩信息
- 将学上信息保存到文件中
- 修改和删除学生信息
- 查询学生信息
- 根据学生成绩进行排序
- 统计学生的总分
打包成exe文件可执行文件
- 安装pip install PyInstaller
- cmd中输入pyinstaller -F E:....test.py
即可打包成可执行文件
test文件
import os
fileName = 'student.txt'
def menum():
print('===============学生信息管理系统=====================')
print('===================功能菜单========================')
print('\t\t1,录入学生信息')
print('\t\t2,查找学生信息')
print('\t\t3,删除学生信息')
print('\t\t4,修改学生信息')
print('\t\t5,排序')
print('\t\t6,统计所有学生总人数')
print('\t\t7,显示所有学生信息')
print('\t\t0,推出系统')
def main():
while True:
menum()
choice = -1
try:
choice = int(input('please select 0-7'))
except ValueError:
print('输入错误请重新输入')
continue
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('you are quit system?y/n')
if answer.lower() == 'y':
print('thank you bye')
break
else:
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
def insert():
student_list = []
while True:
id = int(input('please input ID'))
if not id:
break
name = input('please input NAME')
if not name:
break
try:
english = int(input('please input english'))
python = int(input('please input python'))
java = int(input('please input python'))
except ValueError:
print('input error please Reenter')
continue
student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
student_list.append(student)
answer = input('continue add input y or n \n')
if answer.lower() == 'n':
break
# 调用save函数
save(student_list)
print('student commit ok')
pass
def save(student_list):
stu_txt = None
try:
stu_txt = open(fileName, 'a', encoding='utf-8')
except:
stu_txt = open(fileName, 'w', encoding='utf-8')
for item in student_list:
stu_txt.write(str(item) + '\n')
stu_txt.close()
def search():
student_query = []
while True:
id = ''
name = ''
if os.path.exists(fileName):
mode = input('is id please 1,is name please 2')
if mode == '1':
id = input('please input id')
elif mode == '2':
name = input('please input name')
else:
print('you input error')
search()
with open(fileName, 'r', encoding='utf-8') as rfile:
student = rfile.readlines()
for item in student:
d = dict(eval(item))
if id:
if d['id'] == int(id):
student_query.append(d)
pass
elif name:
if d['name'] == name:
student_query.append(d)
show_student(student_query)
student_query.clear()
answer = input('continue search y/n')
if answer == 'y':
continue
else:
break
pass
else:
print('null student info')
pass
def show_student(lst):
if len(lst) == 0:
print('no search info')
return
else:
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID', 'NAME', 'EN', 'PY', 'JA', 'SUM'))
# 定义内容展示格式
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
for item in lst:
print(format_data.format(item.get('id'), item.get('name'), item['english'], item.get('python'),
item.get('java'),
int(item.get('english')) + int(item.get('python'))+int(item.get('java'))
))
def delete():
while True:
studentId = int(input('please input ID: '))
if studentId != 0:
old = False
if os.path.exists(fileName):
with open(fileName, 'r', encoding='utf-8') as dd:
old = dd.readlines()
else:
ord = []
flag = False # 标记是否删除
if old:
with open(fileName, 'w', encoding='utf-8') as wfile:
d = {}
for item in old:
d = dict(eval(item)) # 将字符串转换成字典
if d['id'] != studentId:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print('id is{0} student info delete ok'.format(studentId))
else:
print(f'no search id{studentId} student info')
show() # 删除之后重新显示学生信息
answer = input('continue delete student info')
if answer.lower() != 'y':
break
else:
print('no student info')
break
else:
print('input error')
pass
def modify():
show()
if os.path.exists(fileName):
with open(fileName, 'r', encoding='utf-8') as rfile:
student_list = rfile.readlines()
else:
return
flag = True
student_id = int(input('please input ID'))
with open(fileName, 'w', encoding='utf-8') as wfile:
for item in student_list:
d = dict(eval(item))
if d['id'] == student_id:
flag = False
print('search student info yes')
while True:
try:
d['name'] = input('please input name')
d['english'] = input('please input english')
d['python'] = input('please input python')
d['java'] = input('please input java')
except ValueError:
print('you input error')
else:
break
wfile.write(str(d) + '\n')
print('update success')
else:
wfile.write(str(d) + '\n')
if flag:
print('fail search')
answer = input('continue student info y/n ')
if answer.lower() == 'y':
modify()
pass
def sort():
show()
if os.path.exists(fileName):
with open(fileName,'r',encoding='utf-8') as rfile:
student_list=rfile.readlines()
student_new=[]
for item in student_list:
d=dict(eval(item))
student_new.append(d)
else:
return
asc_or_desc=input('please select 1 up,2 don')
if asc_or_desc=='1':
asc_or_desc_bool=False
elif asc_or_desc=='2':
asc_or_desc_bool = True
else:
print('input error')
return
mode=input('please select 1.en 2.py 3.ja 4.sum')
if mode=='1':
student_new.sort(key=lambda d:int(d['english']),reverse=asc_or_desc_bool)
pass
elif mode=='2':
student_new.sort(key=lambda d: int(d['python']), reverse=asc_or_desc_bool)
pass
elif mode=='3':
student_new.sort(key=lambda d: int(d['java']), reverse=asc_or_desc_bool)
pass
elif mode=='4':
student_new.sort(key=lambda d: int(d['english'])+int(d['python'])+int(d['java']), reverse=asc_or_desc_bool)
pass
else:
print('input error')
return
show_student(student_new)
def show():
student_lst =[]
if os.path.exists(fileName):
with open(fileName,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
for item in students:
print(item)
student_lst.append(eval(item))
if student_lst:
show_student(student_lst)
else:
print('no student info')
def total():
if os.path.exists(fileName):
with open(fileName,'r',encoding='utf-8')as rfile:
student_lst=rfile.readlines()
if student_lst:
print(f'sum student total: {len(student_lst)}')
else:
print('no student info')
else:
print('no student file')
pass
if __name__ == '__main__':
main()
网友评论