def add_score(student_name, course_id, score): # 根据学生姓名查找学生 student = Student.find(student_name) if student is None: return False, 'Student not found.' # 查找课程 course = Course.find(course_id) if course is None: return False, 'Course not found.' # 创建成绩对象 score = Score(student, course, score) # 判断成绩是否已存在 if score.exist(): return False, 'Score already exists.' # 保存成绩信息 score.save() return True, 'Add score success.'
时间: 2023-11-22 19:51:54 浏览: 43
这段代码是一个添加学生成绩的函数,它接受三个参数:学生姓名、课程ID和成绩。首先会调用 `Student.find` 函数查找该学生是否存在,如果不存在则返回错误信息。然后会调用 `Course.find` 函数查找该课程是否存在,如果不存在则返回错误信息。接着创建一个成绩对象 `score`,并检查该成绩是否已经存在,如果已经存在则返回错误信息。最后调用 `score.save()` 将成绩信息保存到数据库中,并返回成功信息。
相关问题
def query_score(score_dict, student_id):解释一下
`query_score` 是一个函数,接受两个参数 `score_dict` 和 `student_id`。其中 `score_dict` 是一个字典类型,存储了学生的成绩信息,`student_id` 是一个字符串类型,表示要查询的学生的学号。
这个函数的作用是查询学号为 `student_id` 的学生的成绩,并返回查询结果。具体实现方式是通过字典的键值对来查找对应学号的成绩,如果找到则返回成绩,否则返回 `None`。
函数实现的代码如下:
```python
def query_score(score_dict, student_id):
if student_id in score_dict:
return score_dict[student_id]
else:
return None
```
这个函数首先使用 `in` 关键字来判断 `student_id` 是否在 `score_dict` 中,如果存在则返回对应的成绩,否则返回 `None`。
使用这个函数可以方便地查询学生的成绩信息,比如在上面的学号查询和成绩排序系统中调用了这个函数来查询对应学号的成绩:
```python
# 查询成绩
score = query_score(scores, student_id)
if score is None:
print('未找到对应学号的成绩。')
else:
print('学号 %s 的成绩是 %d。' % (student_id, score))
```
这个系统先调用 `query_score` 函数查询学号对应的成绩,如果返回的结果为 `None`,则输出未找到对应学号的成绩,否则输出学号和成绩。
class Student(): # study_time = 10 def __init__(self,name,score): self.name = name self.score = score pass def study(self): # study_time = 4 print("study_time") pass def print_name(self): print(f"学生{self.name()}") def print_score(self): print(f"成绩{self.score()}") def main(): # 实例化对象 需要加() # hwj = Student() # print(hwj.study_time) # hwj.study() # stu2 = {"name": "洪文霁", "score": 99} student1 = Student(name="洪文霁", score=99) student1.print_name() student1.print_score() if __name__ == '__main__': main()
这是对之前代码的修改,将 name 和 score 方法改名为 print_name 和 print_score,避免与属性重名。
在 main 函数中,通过 Student 类实例化了一个对象 student1,并调用了其 print_name 和 print_score 方法输出学生的姓名和分数。代码运行时,会输出:
学生洪文霁
成绩99
说明代码已经正确修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)