帮我用python编个同学录
时间: 2023-07-24 11:13:53 浏览: 176
PHP写同学录
当然可以帮你编写一个同学录的Python程序!下面是一个简单的示例代码:
```python
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Classmates:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def display_classmates(self):
for student in self.students:
print(f"Name: {student.name}, Age: {student.age}, Gender: {student.gender}")
# 创建同学对象
student1 = Student("Alice", 18, "Female")
student2 = Student("Bob", 19, "Male")
student3 = Student("Charlie", 20, "Male")
# 创建同学录对象
classmates = Classmates()
# 添加同学
classmates.add_student(student1)
classmates.add_student(student2)
classmates.add_student(student3)
# 显示同学录
classmates.display_classmates()
```
你可以根据需要扩展该程序,比如添加更多的学生信息或者实现其他功能。希望对你有帮助!如果有任何问题,请随时提问。
阅读全文