编写一个学生和教师数据的输入和显示程序,学生数据有编号、姓名、性别、年龄、班号和成绩
时间: 2024-06-09 14:10:44 浏览: 182
C++小型学生成绩管理系统.docx
以下是一个简单的学生和教师数据的输入和显示程序:
```python
class Student:
def __init__(self, id, name, gender, age, class_no, score):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.class_no = class_no
self.score = score
class Teacher:
def __init__(self, id, name, gender, age, subject):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.subject = subject
students = []
teachers = []
def add_student():
id = input("请输入学生编号:")
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
age = input("请输入学生年龄:")
class_no = input("请输入学生班号:")
score = input("请输入学生成绩:")
student = Student(id, name, gender, age, class_no, score)
students.append(student)
def add_teacher():
id = input("请输入教师编号:")
name = input("请输入教师姓名:")
gender = input("请输入教师性别:")
age = input("请输入教师年龄:")
subject = input("请输入教师科目:")
teacher = Teacher(id, name, gender, age, subject)
teachers.append(teacher)
def show_students():
for student in students:
print("编号:%s,姓名:%s,性别:%s,年龄:%s,班号:%s,成绩:%s" % (student.id, student.name, student.gender, student.age, student.class_no, student.score))
def show_teachers():
for teacher in teachers:
print("编号:%s,姓名:%s,性别:%s,年龄:%s,科目:%s" % (teacher.id, teacher.name, teacher.gender, teacher.age, teacher.subject))
while True:
print("请选择操作:")
print("1. 添加学生")
print("2. 添加教师")
print("3. 显示学生")
print("4. 显示教师")
print("5. 退出")
choice = input()
if choice == "1":
add_student()
elif choice == "2":
add_teacher()
elif choice == "3":
show_students()
elif choice == "4":
show_teachers()
elif choice == "5":
break
else:
print("无效选择,请重新输入。")
```
这个程序定义了一个 `Student` 类和一个 `Teacher` 类,用于存储学生和教师的数据。然后通过 `add_student()` 和 `add_teacher()` 函数分别添加学生和教师数据,通过 `show_students()` 和 `show_teachers()` 函数分别显示所有学生和教师的数据。最后通过一个循环菜单来实现用户的交互操作。
阅读全文