写一个教师通讯录查询系统,要求有显示界面,可操作,有选项。系统里面有:1,显示所有教师 2,按姓名查询 3,按部门查询 4,打印青年教师 5,退出系统,这五个选项。并且生成模拟数据验证通讯录查询系统的准确性
时间: 2024-01-13 20:02:26 浏览: 84
好的,这个教师通讯录查询系统可以使用Python语言进行开发。下面是一个简单的实现:
```python
# 导入模块
import random
# 定义教师类
class Teacher:
def __init__(self, name, department, age):
self.name = name
self.department = department
self.age = age
def __str__(self):
return '{}\t{}\t{}'.format(self.name, self.department, self.age)
# 生成模拟数据
names = ['张三', '李四', '王五', '赵六', '钱七']
departments = ['数学系', '物理系', '化学系', '生物系', '计算机系']
teachers = []
for i in range(50):
name = random.choice(names)
department = random.choice(departments)
age = random.randint(25, 60)
teachers.append(Teacher(name, department, age))
# 显示所有教师
def show_all_teachers():
print('姓名\t部门\t年龄')
for teacher in teachers:
print(teacher)
# 按姓名查询
def search_by_name():
name = input('请输入教师姓名:')
results = [teacher for teacher in teachers if teacher.name == name]
if len(results) == 0:
print('未找到该教师')
else:
print('姓名\t部门\t年龄')
for teacher in results:
print(teacher)
# 按部门查询
def search_by_department():
department = input('请输入部门名称:')
results = [teacher for teacher in teachers if teacher.department == department]
if len(results) == 0:
print('未找到该部门')
else:
print('姓名\t部门\t年龄')
for teacher in results:
print(teacher)
# 打印青年教师
def print_young_teachers():
print('姓名\t部门\t年龄')
for teacher in teachers:
if teacher.age <= 35:
print(teacher)
# 主函数
def main():
while True:
print('1.显示所有教师')
print('2.按姓名查询')
print('3.按部门查询')
print('4.打印青年教师')
print('5.退出系统')
choice = input('请输入选项:')
if choice == '1':
show_all_teachers()
elif choice == '2':
search_by_name()
elif choice == '3':
search_by_department()
elif choice == '4':
print_young_teachers()
elif choice == '5':
break
else:
print('输入有误,请重新输入')
if __name__ == '__main__':
main()
```
这个系统中,通过定义一个教师类,生成了50个随机的教师数据,然后实现了按姓名、按部门查询,以及打印青年教师等功能。用户可以通过输入选项来使用这些功能。在主函数中通过一个while循环,不断接收用户的输入,直到用户选择退出系统。
阅读全文