Python学生管理系统
时间: 2023-11-05 15:50:45 浏览: 104
好的,一个简单的Python学生管理系统可以包括以下功能:
1. 添加学生信息:包括学号、姓名、性别、年龄、联系方式等。
2. 查找学生信息:可以按学号或姓名进行查找,查找到学生信息后可以进行修改或删除操作。
3. 显示所有学生信息:将所有学生的信息以表格形式呈现出来。
4. 统计学生信息:可以统计学生总人数、男女比例、年龄分布等。
下面是一个简单的实现示例:
```python
class Student:
def __init__(self, id, name, gender, age, phone):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.phone = phone
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def find_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def find_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def modify_student(self, student):
old_student = self.find_student_by_id(student.id)
if old_student:
old_student.name = student.name
old_student.gender = student.gender
old_student.age = student.age
old_student.phone = student.phone
def delete_student(self, student):
self.students.remove(student)
def show_all_students(self):
print("学号\t姓名\t性别\t年龄\t联系方式")
for student in self.students:
print("{0}\t{1}\t{2}\t{3}\t{4}".format(student.id, student.name, student.gender, student.age, student.phone))
def count_students(self):
total = len(self.students)
male = len([student for student in self.students if student.gender == "男"])
female = total - male
ages = [student.age for student in self.students]
age_distribution = {}
for age in ages:
if age in age_distribution:
age_distribution[age] += 1
else:
age_distribution[age] = 1
return {
"总人数": total,
"男生人数": male,
"女生人数": female,
"年龄分布": age_distribution
}
```
使用示例:
```python
manager = StudentManager()
# 添加学生信息
s1 = Student("001", "张三", "男", 18, "123456789")
manager.add_student(s1)
s2 = Student("002", "李四", "女", 20, "987654321")
manager.add_student(s2)
# 查找学生信息并修改
s = manager.find_student_by_id("001")
if s:
s.name = "王五"
manager.modify_student(s)
# 查找学生信息并删除
s = manager.find_student_by_name("李四")
if s:
manager.delete_student(s)
# 显示所有学生信息
manager.show_all_students()
# 统计学生信息
count_result = manager.count_students()
print(count_result)
```
阅读全文