gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
现有M(M=2)位教师,N名同学,每名同学有3科成绩(语文,数学,英语),其中一位教师教授2门课程,另一位教师教授1门课程。请随机生成学生的成绩,并实现以下功能。…
- User
现有M(M=2)位教师,N名同学,每名同学有3科成绩(语文,数学,英语),其中一位教师教授2门课程,另一位教师教授1门课程。请随机生成学生的成绩,并实现以下功能。 (一)教师具备的功能 1.查看、修改自己的信息(教师编号不可修改); 2.成绩查询 (1)查询自己教授课程的全班成绩、最高成绩、最低成绩、平均成绩等。 (2)根据学生的学号或姓名,查询该生自己教授课程的成绩。 (3)实现对所教授科目学生成绩的排序,升序降序 3.将学生成绩或个人信息保存到一个文件中 (二)学生具备的功能 1.查看、修改自己的信息(学生学号不可修改); 2.成绩查询 (1)查询自己所有科目的成绩、平均分等。 (2)查询自己某科目的成绩。 3.将个人信息或个人成绩保存到一个文件中 (三)系统管理员 1.对学生、教师的增加、删除、修改、查看。教师编号和学生学号自动生成,不能修改。 2.对成绩的查看(含所有科目,所有学生,单个科目,单个学生的成绩查询等)、排序等。 3.对教师、学生、成绩等内容保存到文件中。
- Assistant
首先,我们需要创建教师、学生和成绩的类来表示他们的信息。
class Teacher: def __init__(self, teacher_id, name, course): self.teacher_id = teacher_id self.name = name self.course = course def view_info(self): print("教师编号:", self.teacher_id) print("姓名:", self.name) print("教授课程:", self.course) def modify_info(self, name, course): self.name = name self.course = course class Student: def __init__(self, student_id, name): self.student_id = student_id self.name = name self.scores = [] def view_info(self): print("学生学号:", self.student_id) print("姓名:", self.name) def modify_info(self, name): self.name = name def add_score(self, score): self.scores.append(score) def get_scores(self): return self.scores class Score: def __init__(self, student_id, course, score): self.student_id = student_id self.course = course self.score = score def view_info(self): print("学生学号:", self.student_id) print("科目:", self.course) print("成绩:", self.score)接下来,我们需要创建一个学校的类来管理教师、学生和成绩,以及实现各种功能。
class School: def __init__(self): self.teachers = [] self.students = [] self.scores = [] def add_teacher(self, teacher): self.teachers.append(teacher) def add_student(self, student): self.students.append(student) def add_score(self, score): self.scores.append(score) def remove_teacher(self, teacher): self.teachers.remove(teacher) def remove_student(self, student): self.students.remove(student) def remove_score(self, score): self.scores.remove(score) def get_teacher_by_id(self, teacher_id): for teacher in self.teachers: if teacher.teacher_id == teacher_id: return teacher return None def get_student_by_id(self, student_id): for student in self.students: if student.student_id == student_id: return student return None def get_scores_by_teacher(self, teacher): teacher_scores = [] for score in self.scores: if score.course == teacher.course: teacher_scores.append(score) return teacher_scores def get_scores_by_student(self, student): student_scores = [] for score in self.scores: if score.student_id == student.student_id: student_scores.append(score) return student_scores def get_scores_by_course(self, course): course_scores = [] for score in self.scores: if score.course == course: course_scores.append(score) return course_scores def get_scores_by_student_and_course(self, student, course): student_scores = [] for score in self.scores: if score.student_id == student.student_id and score.course == course: student_scores.append(score) return student_scores def get_average_score_by_teacher(self, teacher): scores = self.get_scores_by_teacher(teacher) total_score = 0 for score in scores: total_score += score.score if len(scores) > 0: return total_score / len(scores) else: return 0 def get_average_score_by_student(self, student): scores = self.get_scores_by_student(student) total_score = 0 for score in scores: total_score += score.score if len(scores) > 0: return total_score / len(scores) else: return 0 def get_highest_score_by_teacher(self, teacher): scores = self.get_scores_by_teacher(teacher) highest_score = 0 for score in scores: if score.score > highest_score: highest_score = score.score return highest_score def get_lowest_score_by_teacher(self, teacher): scores = self.get_scores_by_teacher(teacher) lowest_score = 100 for score in scores: if score.score < lowest_score: lowest_score = score.score return lowest_score def sort_scores_by_student(self, student, reverse=False): scores = self.get_scores_by_student(student) scores.sort(key=lambda x: x.score, reverse=reverse) return scores def sort_scores_by_course(self, course, reverse=False): scores = self.get_scores_by_course(course) scores.sort(key=lambda x: x.score, reverse=reverse) def save_info_to_file(self, file_name): with open(file_name, "w") as f: for teacher in self.teachers: f.write("Teacher\n") f.write(str(teacher.teacher_id) + "," + teacher.name + "," + teacher.course + "\n") for student in self.students: f.write("Student\n") f.write(str(student.student_id) + "," + student.name + "\n") for score in self.scores: f.write("Score\n") f.write(str(score.student_id) + "," + score.course + "," + str(score.score) + "\n") def load_info_from_file(self, file_name): with open(file_name, "r") as f: lines = f.readlines() for i in range(len(lines)): if lines[i].strip() == "Teacher": teacher_info = lines[i + 1].strip().split(",") teacher = Teacher(int(teacher_info[0]), teacher_info[1], teacher_info[2]) self.add_teacher(teacher) elif lines[i].strip() == "Student": student_info = lines[i + 1].strip().split(",") student = Student(int(student_info[0]), student_info[1]) self.add_student(student) elif lines[i].strip() == "Score": score_info = lines[i + 1].strip().split(",") score = Score(int(score_info[0]), score_info[1], float(score_info[2])) self.add_score(score)以上是根据题目要求实现的类和相关功能。你可以根据需要调用这些类和函数来实现具体的操作。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.