以下就是源码:
# 定义学生类
class Student:
def __init__(self, id, name, age):
self.id = id
self.name = name
self.age = age
# 初始化学生列表
students = [
Student(1, 'Tom', 18),
Student(2, 'Jerry', 19),
Student(3, 'Alice', 20)
]
# 添加学生
def add_student():
id = int(input('请输入学生ID:'))
name = input('请输入学生姓名:')
age = int(input('请输入学生年龄:'))
student = Student(id, name, age)
students.append(student)
print('添加成功!')
# 删除学生
def delete_student():
id = int(input('请输入要删除的学生ID:'))
for student in students:
if student.id == id:
students.remove(student)
print('删除成功!')
return
print('找不到该学生!')
# 修改学生
def modify_student():
id = int(input('请输入要修改的学生ID:'))
for student in students:
if student.id == id:
name = input('请输入学生姓名:')
age = int(input('请输入学生年龄:'))
student.name = name
student.age = age
print('修改成功!')
return
print('找不到该学生!')
# 查询学生
def query_student():
id = int(input('请输入要查询的学生ID:'))
for student in students:
if student.id == id:
print('学生ID:', student.id)
print('学生姓名:', student.name)
print('学生年龄:', student.age)
return
print('找不到该学生!')
#展示学生信息
def show_student():
for student in students:
print('学生ID:', student.id,'学生姓名:', student.name,'学生年龄:', student.age)
return
# 显示菜单
def show_menu():
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生')
print('4. 查询学生')
print('5. 显示信息')
print('6. 退出系统')
# 主函数
def main():
while True:
show_menu()
choice = int(input('请选择操作:'))
if choice == 1:
add_student()
elif choice == 2:
delete_student()
elif choice == 3:
modify_student()
elif choice == 4:
query_student()
elif choice == 5:
show_student()
elif choice == 6:
break
else:
print('无效的选择!')
if __name__ == '__main__':
main()