示例:在工具框中,包含着本问所要实现的功能,输入每一个功能前的数字,即可实现这个功能
代码如下:
import os
代码如下:
if __name__ == '__main__':
main()
规定函数的执行次序
代码如下:
def main():
while True:
menm()
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
if answer == 'n' or answer == 'N':
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 menm():
print('''
==============================学生信息管理系统===================================
---------------------------------功能选择---------------------------------------
\t\t\t\t 1.录入学生信息
\t\t\t\t 2.查找学生信息
\t\t\t\t 3.删除学生信息
\t\t\t\t 4.修改学生信息
\t\t\t\t 5.排序
\t\t\t\t 6.统计学生总人数
\t\t\t\t 7.显示所有学生信息
\t\t\t\t 0.退出系统
------------------------------------------------------------------------------
==============================================================================
''')
定义要显示主页面,列出所有的功能
代码如下:
def insert():
student_list = []
while True:
id = input("请输入id(比如1001):")
if not id:
break
name = input("请输入姓名:")
if not name:
break
try:
escore = int(input("请输入英语成绩:"))
java = int(input("请输入java成绩:"))
python = int(input("请输入python成绩:"))
except:
print("输入有误,请重新输入")
continue
student = {
'id':id,'name':name,'escore':escore,'java':java,'python':python}
student_list.append(student)
answer = input("是否继续输入y/n:")
if answer == 'y' or answer == 'Y':
continue
else:
break
save(student_list)
print("学生信息录入完毕!!!")
代码如下:
filename = 'student.txt'
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()
save函数,把得到的信息写入到规定的文件里
代码如下:
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)
if 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
查询已保存的学生信息,可根据ID或姓名查询
代码如下:
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','姓名','英语成绩','java成绩','python成绩','总成绩'))
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.get('escore'),
item.get('java'),
item.get('python'),
int(item.get('escore'))+int(item.get('java'))+int(item.get('python'))
))
展示搜索到的学生信息,并按照规定格式显示
代码如下:
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()
s = print('请选择是否继续删除学生信息 y/n:')
if s == 'y' or s == 'Y':
continue
else:
break
))
把已保存的学生信息,按照输入的ID删除此学生信息
代码如下:
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(f'找到{student_id}的信息,可以修改')
while True:
try:
d['name'] = input('请输入名字:')
d['escore'] = int(input('请输入英语成绩:'))
d['java'] = input('请输入java成绩:')
d['python'] = input('请输入python成绩:')
except:
print('输入有误,请重新输入')
continue
else:
break
wfile.write(str(d)+'\n')
else:
wfile.write(str(d)+'\n')
wfile.close()
answer = input('是否继续修改学生信息 y/n:')
if answer == 'y' or answer == 'Y':
modify()
else:
print("信息修改成功!!!")
把已保存的学生信息,重新修改在保存
代码如下:
def sort():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
student_new = []
for item in students:
d = dict(eval(item))
student_new.append(d)
asc = input('请选择(0.升序 1.降序):')
if asc == '0':
asc_bool = False
elif asc == '1':
asc_bool = True
else:
print('您的输入有误,请重新输入!!!')
sort()
mode = input('请选择排序方式(1.按英语成绩排序 2.按java成绩排序 3.按python成绩排序 0.按总成绩排序):')
if mode == '1':
student_new.sort(key=lambda x:int(x['escore']),reverse= asc_bool)
elif mode == '2':
student_new.sort(key=lambda x:int(x['java']),reverse= asc_bool)
elif mode == '3':
student_new.sort(key=lambda x:int(x['python']),reverse= asc_bool)
elif mode == '0':
student_new.sort(key=lambda x:int(x['escore'])+int(x['python'])+int(x['java']),reverse= asc_bool)
else:
print('输入有误,请重新输入!!!')
sort()
show_student(student_new)
else :
return
))
根据各科的成绩,来进行升序或降序排列,并显示排序后得信息
代码如下:
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_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)
))
展示所有已保存的学生信息
提高对python基础的掌握