一、功能介绍
实现了控制台版本的学生管理系统,包含基本的增删改查四个功能
二、效果图
1.进入系统
2.查看所有学生
3.添加学生
4.再次查看所有学生
5.删除学生
三、代码实现
# coding:utf-8
student_list = []
def show_all_student():
"""查看所有学生信息"""
if len(student_list) == 0:
print('当前学生数量为0')
return
print('---------------------------------------------')
for student in student_list:
print('学号:{}\t 姓名:{}\t 性别:{}\t 年龄:{}\t'.format(student['student_id'], student['name'], student['sex'],
student['age']))
print('---------------------------------------------')
def add_student():
"""添加学生信息"""
student_id = int(input('>>>请输入学生学号:'))
name = input('>>>请输入学生姓名:')
age = int(input('>>>请输入学生年龄:'))
sex = input('>>>请输入学生性别:')
if isinstance(student_id, int) and isinstance(age, int):
for stu in student_list:
if stu['student_id'] == student_id:
return '失败,学号已经存在!'
if sex not in ['男', '女']:
return '!!!!!!失败,非法性别!'
item = dict()
item['student_id'] = student_id
item['name'] = name
item['age'] = age
item['sex'] = sex
student_list.append(item)
return '***** 恭喜!添加成功!*****'
def update_student():
"""修改学生信息"""
item = dict()
student_id = int(input('>>>>请输入学生id:'))
if isinstance(student_id, int):
for student in student_list:
if student['student_id'] == student_id:
item = student
continue
if len(item) == 0:
return '学生不存在!'
else:
print('--------------------------------------------')
print('学号:{}\t 姓名:{}\t 性别:{}\t 年龄:{}\t'.format(item['student_id'], item['name'], item['sex'],
item['age']))
print('--------------------------------------------')
name = input('>>>请重新输入学生姓名:')
age = int(input('>>>请输入重新学生年龄:'))
sex = input('>>>请重新输入学生性别:')
if isinstance(age, int) is False:
return '失败!年龄输入非法!'
if sex not in ['男', '女']:
return '!!!!!!失败,非法性别!!!!!!'
new_item = dict()
new_item['name'] = name
new_item['age'] = age
new_item['sex'] = sex
item.update(new_item)
print('-------------------修改成功-------------------------')
print('学号:{}\t 姓名:{}\t 性别:{}\t 年龄:{}\t'.format(item['student_id'], item['name'], item['sex'],
item['age']))
print('----------------------------------------------------')
return ' '
def del_student():
"""删除学生信息"""
item = dict()
student_id = int(input('>>>>请输入学生id:'))
if isinstance(student_id, int):
for student in student_list:
if student['student_id'] == student_id:
item = student
continue
if len(item) == 0:
return '学生不存在!'
else:
print('--------------------------------------------')
print('学号:{}\t 姓名:{}\t 性别:{}\t 年龄:{}\t'.format(item['student_id'], item['name'], item['sex'],
item['age']))
print('--------------------------------------------')
num = int(input('>>>删除请按【1】,退回请按其他键:'))
if num == 1 and isinstance(num, int):
student_list.remove(item)
return '成功删除!'
else:
return '返回主界面'
def show_one_student():
"""查找单个学生"""
item = dict()
student_id = int(input('>>>>请输入学生id:'))
if isinstance(student_id, int):
for student in student_list:
if student['student_id'] == student_id:
item = student
continue
if len(item) == 0:
return '学生不存在!'
else:
print('--------------------------------------------')
print('学号:{}\t 姓名:{}\t 性别:{}\t 年龄:{}\t'.format(item['student_id'], item['name'], item['sex'],
item['age']))
print('--------------------------------------------')
return '返回主界面'
if __name__ == '__main__':
print('************ 欢迎进入学生管理系统 ************')
while True:
print('\n************* 【1】查看所有学生 ')
print('************* 【2】添加学生信息')
print('************* 【3】修改学生信息 ')
print('************* 【4】查找学生信息 ')
print('************* 【5】删除学生信息 ')
print('************* 【0】退出系统')
num = int(input('>>>请输入指令:'))
if num == 1:
show_all_student()
elif num == 2:
print(add_student())
elif num == 3:
print(update_student())
elif num == 4:
print(show_one_student())
elif num == 5:
print(del_student())
elif num == 0:
print('----------------再见!------------------')
break
else:
print('!!!!!!!!!!操作有误!!!!!!!!!!')