代码如下:
studentList = [] # 保存学生信息的列表
def menu():
# 输出菜单
print('''
学生信息管理系统
=============== 功能菜单 ===============
│ 1 录入学生信息 │
│ 2 查找学生信息 │
│ 3 删除学生信息 │
│ 4 修改学生信息 │
│ 5 排序 │
│ 6 统计学生总人数 │
│ 7 显示所有学生信息 │
│ 0 退出系统
''')
def main():
ctrl=True
while ctrl:
menu()
opion_str=int(input('请选择>>>'))
if opion_str==0:
print('exit')
ctrl=False
elif opion_str==1:
insert()
elif opion_str==2:
search()
elif opion_str==3:
delete()
elif opion_str==4:
modify()
elif opion_str==5:
sort()
elif opion_str==6:
total()
elif opion_str == 7:
show_student()
def insert():
mark=True
while mark:
id_=input('请输入id:')
name_=input('请输入name:')
English=int(input('请输入英语成绩:'))
python=int(input('请输入python成绩:'))
C_=int(input('请输入c成绩:'))
student={"id_": id_ , 'name_': name_ , "English": English , "Python": python, "C": C_}
studentList.append(student)
y=input('是否继续要添加:y/n >>>')
if y=='y':
mark=True
else:
mark=False
def search():
global id, name # id、name在for内使用需要在for外声明
mark=True
while mark:
mode=input('id查找按1,name查找按2 >>> ')
if mode=='1':
id =input('请输入id:')
else:
name =input('请输入name:')
student_query=[]
for d in studentList: # for内使用id、name时需要在外部声明,否则python认为id、name是for内没有定义的局部变量,会报错。
if d["id_"] == id:
student_query.append(d)
elif d["name_"] == name:
student_query.append(d)
show_student(student_query)
student_query.clear()
y=input('是否继续查找:y/n >>>')
if y=='y':
mark=True
else:
mark=False
def delete():
mark=True
while mark:
del_name=input('请输入要删除的name:')
for d in studentList:
if d['name_']== del_name :
studentList.remove(d)
show_student(studentList)
y = input('是否继续删除:y/n >>>')
if y == 'y':
mark = True
else:
mark = False
def modify():
mark=True
while mark:
name1=input('请输入要修改的name >>>')
for d in studentList:
if d['name_'] == name1:
sub_ject=input('请输入要修改的类型:id、English score、Python score、C score>>>')
if sub_ject=='id':
id2=input('请输入修改后的id:')
d['id_']=id2
show_student(studentList)
elif sub_ject=='English score':
E_score=int(input('请输入修改后的english score:'))
d['English']=E_score
show_student(studentList)
elif sub_ject=='Python score':
P_score=int(input('请输入修改后的Python score:'))
d['Python']=P_score
show_student(studentList)
elif sub_ject=='C score':
C_score=int(input('请输入修改后的C score:'))
d['C']=C_score
show_student(studentList)
else:
print('未找到对应类型'.center(30, '*'))
else:
print('未找到对应name'.center(30,'*'))
show_student(studentList)
y = input('是否继续修改学生信息:y/n >>>')
if y == 'y':
mark = True
else:
mark = False
def sort():
mode = input("请选择排序方式(1按英语成绩排序;2按Python成绩排序;3按C语言成绩排序;0按总成绩排序):")
if mode == "1": # 按英语成绩排序
studentList.sort(key=lambda x: x["English"])
elif mode == "2": # 按Python成绩排序
studentList.sort(key=lambda x: x["Python"])
elif mode == "3": # 按C语言成绩排序
studentList.sort(key=lambda x: x["C"])
elif mode == "0": # 按总成绩排序
studentList.sort(key=lambda x: x["English"] + x["Python"] + x["C"])
else:
print("您的输入有误,请重新输入!")
sort()
show_student() # 显示排序结果
def total():
count=0
for d in studentList:
count+=1
print('一共有%d个学生'.center(30,'*')%(count))
#将studentList = [] # 保存学生信息的列表 显示出来
def show_student(studentList=studentList):
from prettytable import PrettyTable
column=['id','name','English score','python score','c score','sum score']
table= PrettyTable(field_names=column)
for info in studentList: #info是一个字典
sum_score=info.get('English')+info.get('Python')+info.get('C') #get获取对应value值
row=list(info.values()) #info.values()为([1,2,3])所有对应value值组成列表放入元组中
row.append(sum_score) #将总成绩添加到列表中
table.add_row(row)#按行添加value值
print(table)
if __name__ == '__main__':
main()
time.sleep(3600)
terminal输入:pyinstaller -F -i 图标.ico 学生信息管理系统.py