(在学C语言的时候也学过哈哈哈,直接给代码了,记录一下!)
import os.path
filename = 'student.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() # 显示所有学生信息
else:
print('参数不在范围内,请重新输入参数!')
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():
student_list = []
while True:
id = input('请输入学生ID(如101):')
if not id:
break
name = input('请输入学生姓名:')
if not name:
break
try:
english = float(input('请输入英语成绩:'))
python = float(input('请输入Python成绩:'))
math = float(input('请输入数学成绩:'))
except:
print('输入无效成绩,请重新输入')
continue
# 将信息保存到字典中(C语言中的是结构体)
student = {'id': id, 'name': name, 'English': english, 'Python': python, 'Math': math}
# 将学生信息添加到列表中
student_list.append(student)
answer = input('是否继续添加学生信息(y/n):')
if answer == 'y':
continue
else:
break
# 调用save函数保存学生信息
save(student_list)
print('学生信息录入完毕!')
def save(student_list):
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('按ID查找请输入1,按姓名查找请输入2:')
if mode == '1':
id = input('请输入学号:')
elif mode == '2':
name = input('请输入姓名:')
else:
print('输入有误,请重新输入!')
continue
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' or answer == 'Y':
continue
else:
break
else:
print('暂未保存学生信息')
return
def show_student(student_list):
if len(student_list) == 0:
print('没有查询到学生信息,无数据显示!')
return
# 定义标题显示格式
format_tile = '{:^6}\t{:^10}\t{:^8}\t{:^10}\t{:^8}\t{:^12}' # ^表示居中
print(format_tile.format('ID', '姓名', '英语成绩', 'Python成绩', '数学成绩', '总成绩'))
# 定义内容的显示格式
format_data = '{:^6}\t{:^10}\t{:^10}\t{:^10}\t{:^10}\t\t{:.2f}'
for item in student_list:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('English'),
item.get('Python'),
item.get("Math"),
float(item.get('English')) + float(item.get('Python')) + float(item.get('Math'))))
def delete():
while True:
student_id = input('请输入要删除的学生ID:')
if student_id != ' ': # 如果输入不为空,则进入下一步
if os.path.exists(filename): # 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('请输入要修改的学号:')
with open(filename, 'a', 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'] = int(input('请输入英语成绩:'))
d['Python'] = int(input('请输入Python成绩:'))
d['Math'] = int(input('请输入数学成绩:'))
except:
print('输入有误,请重新输入!')
else:
break
wfile.write(str(d) + '\n')
print('修改成功!')
elif d['id'] != student_id:
print('没找到该学生信息,请重新输入!')
break
answer = input('是否继续修改学生信息?(y/n):')
if answer == 'y' or 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('输入有误,请重新输入!') # 可以用while True把input这块包起来,if和elif最后加break,else最后加continue
sort()
mode = int(input('请选择排序方式(1.按英语成绩排序 2.按Python成绩排序 3.按数学成绩排序 4.按总成绩排序):'))
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['Math']), reverse=asc_or_desc_bool)
elif mode == 4:
student_new.sort(key=lambda x: float(x['English'] + x['Python'] + x['Math']), 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('一共有{}名学生!'.format(len(students)))
else:
print('还没有录入学生信息!')
else:
print('暂未保存数据信息...')
def show():
student_list = []
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
for item in students:
student_list.append(eval(item))
if student_list:
show_student(student_list)
else:
print('还没有录入学生信息!')
else:
print('暂未保存数据信息...')
if __name__ == '__main__':
main()
项目打包成.exe文件
首先得下载好pip
然后打开cmd输入:pip install PyInstaller
安装好后,再输入:pyinstaller -F D:\py_works\studentsys\stusystem.py(加粗部分是要转为.exe文件的路径)
如图就把.py项目转化为.exe文件。