- 最终效果
- 系统设计
- 主函数设计
- 源码
- 学生信息
- 打包
# 2022/3/28 14:10
import os.path
filename = 'student_info.txt'
def main():
while True:
menu()
choice = int(input('请选择:'))
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('您确定要退出系统吗?y/n:')
if answer == 'y' or answer == 'Y':
print('谢谢您的使用!!!')
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 menu():
print('===================学生信息管理系统===================')
print('---------------------功能菜单------------------------')
print('\t\t\t\t\t\t1.录入学生信息')
print('\t\t\t\t\t\t2.查找学生信息')
print('\t\t\t\t\t\t3.删除学生信息')
print('\t\t\t\t\t\t4.修改学生信息')
print('\t\t\t\t\t\t5.排序学生信息')
print('\t\t\t\t\t\t6.统计学生总人数')
print('\t\t\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t\t\t0.退出系统')
print('-----------------------------------------------------')
def insert():
sutdent_list = []
while True:
id = input('请输入ID(如1001):')
if not id:
break
name = input('请输入姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩:'))
python = int(input('请输入Python成绩:'))
java = int(input('请输入Java成绩:'))
except:
print('请输入整数类型成绩!')
continue
# 将信息保存到字典中
student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
sutdent_list.append(student)
answer = input('是否继续添加?y/n:')
if answer == 'y' or answer == 'Y':
continue
else:
break
save(sutdent_list)
print('学生信息录入完毕!!!')
# 将学生信息写入磁盘
def save(lst):
try:
stu_txt = open(filename, 'a', encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in lst:
stu_txt.write(str(item) + '\n')
stu_txt.close()
def search():
student_query = []
while True:
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', encoding='utf-8') as rfile:
student = rfile.readlines()
for item in student:
d = dict(eval(item))
if id != '':
if d['id'] == id:
student_query.append(d)
elif name != '':
if d['name'] == name:
student_query.append(d)
# 显示查询结果
show_student(student_query)
# 清空列表
student_query.clear()
answer = input('是否继续查询?y/n:')
if answer == 'y':
continue
else:
break
else:
print('无相关信息!')
return
def show_student(lst):
if len(lst) == 0:
print('没有查到相关数据!')
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID', '姓名', '英语成绩', 'Python成绩', 'Java成绩', '总成绩'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
for item in lst:
print(format_data.format(
item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
int(item.get('english')) + int(item.get('python')) + int(item.get('java'))
))
# 删除学生信息
def delete():
while True:
student_id = input('请输入要删除学生的ID:')
if student_id != '':
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
student_old = []
flag = False # 标记是否删除
if student_old:
with open(filename, 'w', encoding='utf-8') as wfile:
d = {}
for item in student_old:
d = dict(eval(item)) # 将字符串转为字典
if d['id'] != student_id:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print(f'ID为{student_id}的学生信息已被删除!')
else:
print(f'没有找到ID为{student_id}的学生信息!')
else:
print('没有找到相关学生信息!')
break
show() # 删除之后重新显示所有学生信息
answer = input('是否继续删除?y/n:')
if answer == 'y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
return
student_id = input('请输入要修改的学生ID:')
with open(filename, 'w', encoding='utf-8') as wfile:
for item in student_old:
d = dict(eval(item))
if d['id'] == student_id:
print('请修改:')
while True:
try:
d['name'] = input('请输入姓名:')
d['english'] = input('请输入英语成绩:')
d['python'] = input('请输入python成绩:')
d['java'] = input('请输入Java成绩:')
except:
print('输入有误,请重新输入!')
else:
break
wfile.write(str(d) + '\n')
print('修改成功!!!')
else:
print('未找到学生信息!')
wfile.write(str(d) + '\n')
answer = input('是否继续修改其他学生的信息?y/n:')
if answer == 'y':
modify()
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('请选择(0:升序 1:降序):')
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('输入有误,请重新输入!')
sort()
mode = input('请选择(1.按英语成绩排序 2.按Python成绩排序 3.按Java成绩排序 0.按总成绩排序):')
if mode == '1':
student_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)
elif mode == '2':
student_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
elif mode == '3':
student_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
elif mode == '0':
student_new.sort(key=lambda x: int(x['english']) + int(x['python']) + int(x['java']), reverse=asc_or_desc_bool)
else:
print('输入有误,请重新输入!')
sort()
show_student(student_new)
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
print(f'一共有 {len(students)} 名学生')
else:
print('没有录入学生信息!')
else:
print('暂未保存学生信息!')
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:
student_lst.append(eval(item))
if student_lst:
show_student(student_lst)
else:
print('暂未保存数据!')
# 如果要打包以exe文件运行,则必须有主函数
if __name__ == '__main__':
main()
学生信息
学生信息存放于 student_info.txt
{'id': '1002', 'name': '李四', 'english': '78', 'python': '89', 'java': '100'}
{'id': '1001', 'name': '张三', 'english': '100', 'python': '100', 'java': '99'}
{'id': '1003', 'name': '王五', 'english': '88', 'python': '99', 'java': '95'}
打包
安装打包工具:pip install PyInstaller
打包:pyinstaller -F 要打包的python文件
,例如:pyinstaller -F D:\stusystem.py
Tips:打包后生成的exe文件存放于当前python文件目录下,双击即可运行!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)