class Person: def __init__(self, last_name, first_name, gender, birthday): self.last_name = last_name self.first_name = first_name self.gender = gender self.birthday = birthday class Student(Person): def __init__(self, last_name, first_name, gender, birthday, student_id, major): super().__init__(last_name, first_name, gender, birthday) self.student_id = student_id self.major = major self.selected_courses = {} def select_course(self, course): self.selected_courses = course def modify_course(self, course_id): del self.selected_courses[course_id] def f2(): print('''If you want to get the information of selected courses, choose number 1. If you want to modify selected courses, choose number 2.''') choice1 = input('Please make your choice:') student_id = input('Please input your student_id:') student = basic_class.Student('', '', '', '', student_id, '') if choice1 == '1': course_id1 = input('Please input the course_id you want to query:') basic_class.Student.get_information(student, course_id1) if choice1 == '2': choice11 = input('Choose your following operation(1.add;2.delete):') course_id2 = input('Please input the course_id you want to query:') if choice11 == '1': basic_class.Student.select_course(student, course_id2) if choice11 == '2': basic_class.Student.modify_course(student, course_id2) 找出这段代码的错误并修正
时间: 2023-06-18 20:07:13 浏览: 116
python基础进阶1.6:面向对象之类,对象及__init__(),self相关用法讲解
代码中存在以下错误:
1. 在调用方法时,应该使用对象调用方法,而不是类名调用方法。
2. 在子类 `Student` 中,没有定义 `get_information` 方法。
3. 在调用 `modify_course` 方法时,应该传递课程编号 `course_id2`,而不是学生对象 `student`。
以下是修正后的代码:
```
class Person:
def __init__(self, last_name, first_name, gender, birthday):
self.last_name = last_name
self.first_name = first_name
self.gender = gender
self.birthday = birthday
class Student(Person):
def __init__(self, last_name, first_name, gender, birthday, student_id, major):
super().__init__(last_name, first_name, gender, birthday)
self.student_id = student_id
self.major = major
self.selected_courses = {}
def select_course(self, course):
self.selected_courses[course.course_id] = course
def modify_course(self, course_id):
del self.selected_courses[course_id]
def get_information(self, course_id):
if course_id in self.selected_courses:
print(f"Course ID:{course_id}, Course Name:{self.selected_courses[course_id].name}, Course Credit:{self.selected_courses[course_id].credit}")
else:
print("The selected course does not exist.")
class Course:
def __init__(self, course_id, name, credit):
self.course_id = course_id
self.name = name
self.credit = credit
def f2():
print('''If you want to get the information of selected courses, choose number 1. If you want to modify selected courses, choose number 2.''')
choice1 = input('Please make your choice:')
student_id = input('Please input your student_id:')
student = Student('', '', '', '', student_id, '')
if choice1 == '1':
course_id1 = input('Please input the course_id you want to query:')
student.get_information(course_id1)
if choice1 == '2':
choice11 = input('Choose your following operation(1.add;2.delete):')
course_id2 = input('Please input the course_id you want to query:')
if choice11 == '1':
course_name = input('Please input the course name:')
course_credit = input('Please input the course credit:')
course = Course(course_id2, course_name, course_credit)
student.select_course(course)
if choice11 == '2':
student.modify_course(course_id2)
```
阅读全文