编写一个学生和教师数据输入和显示程序,学生数据有学号、姓名、班号和成绩;教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生类student和teacher类的基类
时间: 2024-04-03 17:31:09 浏览: 65
好的,我可以帮您编写这个程序。以下是代码示例:
```python
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
class Student(Person):
def __init__(self, id, name, class_id, score):
super().__init__(id, name)
self.class_id = class_id
self.score = score
class Teacher(Person):
def __init__(self, id, name, title, department):
super().__init__(id, name)
self.title = title
self.department = department
# 输入学生数据
student_list = []
while True:
id = input("请输入学生学号(输入q结束输入):")
if id == 'q':
break
name = input("请输入学生姓名:")
class_id = input("请输入学生班级:")
score = input("请输入学生成绩:")
student = Student(id, name, class_id, score)
student_list.append(student)
# 输入教师数据
teacher_list = []
while True:
id = input("请输入教师编号(输入q结束输入):")
if id == 'q':
break
name = input("请输入教师姓名:")
title = input("请输入教师职称:")
department = input("请输入教师所在部门:")
teacher = Teacher(id, name, title, department)
teacher_list.append(teacher)
# 显示学生数据
print("学生数据:")
for student in student_list:
print("学号:{},姓名:{},班级:{},成绩:{}".format(student.id, student.name, student.class_id, student.score))
# 显示教师数据
print("教师数据:")
for teacher in teacher_list:
print("编号:{},姓名:{},职称:{},部门:{}".format(teacher.id, teacher.name, teacher.title, teacher.department))
```
这段代码定义了一个`Person`类作为`Student`和`Teacher`类的基类,分别表示学生和教师信息。在输入学生和教师信息时,我们通过`input`函数获取用户输入,并将输入内容传入相应的类的构造函数中创建对象。最后,我们通过`print`函数输出学生和教师的信息。
阅读全文