class Course: def __init__(self, name, credit, time): self.name = name self.credit = credit self.time = time self.courses = [] def add_course(self,course): self.courses.append(course) def print_courses(self): for course in self.courses: print(course.name) def choose_class(): print(''' 1.'Advanced Mathematics', 'credit:4', 'Monday 8:30-10:10')) 2.'College English', 'credit:3', 'Tuesday 2:30-4:10' 3.'Computer programming', 'credit:4', 'Wednesday 10:30-12:10' 4.'Computer Design', 'credit:3', 'Thursday 4:30-6:10''') ch = int(input('Please choose the class that you want:')) if ch == 1: Course.add_course(Course('Advanced Mathematics', 4, 'Monday 8:30-10:10')) elif ch == 2: Course.add_course(Course('College English', 3, 'Tuesday 2:30-4:10')) elif ch == 3: Course.add_course(Course('Computer programming', 4, 'Wednesday 10:30-12:10')) elif ch == 4: Course.add_course(Course('Computer Design', 3, 'Thursday 4:30-6:10'))
时间: 2023-06-16 10:08:02 浏览: 62
这段代码定义了一个课程类 `Course`,包含了课程名称、学分和上课时间等属性,以及添加课程和打印课程的方法。另外还定义了一个 `choose_class()` 函数,让用户选择课程并添加到课程列表中。
但是,这段代码存在几个问题:
1. `courses` 属性应该在类的外部定义,而不是在 `__init__` 方法中定义。否则每次实例化对象时都会创建一个新的空列表,而无法保留已经添加的课程。
2. 在 `choose_class()` 函数中,应该先创建一个 `Course` 对象,再将其添加到课程列表中。而不是直接调用 `add_course()` 方法,因为该方法需要先获取对应的 `Course` 对象。
3. 在 `choose_class()` 函数中,应该加上异常处理,防止用户输入不合法的选项。
修改后的代码如下:
```
class Course:
courses = []
def __init__(self, name, credit, time):
self.name = name
self.credit = credit
self.time = time
def add_course(self,course):
self.courses.append(course)
def print_courses(self):
for course in self.courses:
print(course.name)
def choose_class():
print('''1. 'Advanced Mathematics', 'credit:4', 'Monday 8:30-10:10'
2. 'College English', 'credit:3', 'Tuesday 2:30-4:10'
3. 'Computer programming', 'credit:4', 'Wednesday 10:30-12:10'
4. 'Computer Design', 'credit:3', 'Thursday 4:30-6:10' ''')
ch = input('Please choose the class that you want:')
try:
ch = int(ch)
if ch == 1:
course = Course('Advanced Mathematics', 4, 'Monday 8:30-10:10')
elif ch == 2:
course = Course('College English', 3, 'Tuesday 2:30-4:10')
elif ch == 3:
course = Course('Computer programming', 4, 'Wednesday 10:30-12:10')
elif ch == 4:
course = Course('Computer Design', 3, 'Thursday 4:30-6:10')
else:
raise ValueError
Course.courses.append(course)
except ValueError:
print("Invalid option")
choose_class()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)