学生信息管理系统python初学者
时间: 2023-12-28 15:05:31 浏览: 91
以下是一个简单的学生信息管理系统的Python代码示例,适合初学者使用:
```python
class Student:
def __init__(self, name, student_id, phone_number):
self.name = name
self.student_id = student_id
self.phone_number = phone_number
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, student_id, phone_number):
# 检测学号是否重复/有误
for student in self.students:
if student.student_id == student_id:
print("学号已存在,请重新输入!")
return
if not student_id.isdigit():
print("学号必须为纯数字,请重新输入!")
return
# 检测手机号是否合法
if not phone_number.isdigit() or len(phone_number) != 11:
print("手机号格式不正确,请重新输入!")
return
student = Student(name, student_id, phone_number)
self.students.append(student)
print("学生信息添加成功!")
def display_students(self):
for student in self.students:
print("姓名:", student.name)
print("学号:", student.student_id)
print("手机号:", student.phone_number)
print()
# 创建学生管理系统对象
sms = StudentManagementSystem()
# 添加学生信息
sms.add_student("张三", "1001", "13812345678")
sms.add_student("李四", "1002", "13987654321")
# 显示学生信息
sms.display_students()
```
阅读全文