编写一个函数getGrade(score),score是分数。90~100返回A,80~89返回B,60-79返回C,0~60返回D,其他返回error。
时间: 2024-02-28 17:57:09 浏览: 23
好的,我可以为您编写这个函数。以下是 Python 语言的实现代码:
```python
def getGrade(score):
if score >= 90 and score <= 100:
return 'A'
elif score >= 80 and score <= 89:
return 'B'
elif score >= 60 and score <= 79:
return 'C'
elif score >= 0 and score <= 60:
return 'D'
else:
return 'error'
```
这个函数接收一个参数 score,根据分数的范围返回不同的等级。如果分数不在指定范围内,则返回 "error"。您可以根据需要调用这个函数,比如:
```python
print(getGrade(85)) # 输出 B
```
相关问题
c++编写一个函数,按所给的百分制的成绩分数,返回与该分数对应的等级代号字符
好的,我可以为您编写这个函数。以下是代码示例:
```c++
#include <iostream>
using namespace std;
char getGrade(int score) {
if (score >= 90 && score <= 100) {
return 'A';
} else if (score >= 80 && score < 90) {
return 'B';
} else if (score >= 70 && score < 80) {
return 'C';
} else if (score >= 60 && score < 70) {
return 'D';
} else {
return 'E';
}
}
int main() {
int score = 85;
char grade = getGrade(score);
cout << "分数为:" << score << ",等级为:" << grade << endl;
return 0;
}
```
在这个函数中,我们通过传入一个百分制的分数,判断该分数对应的等级代号字符,并将其返回。在主函数中,我们将分数传入该函数,并将返回的等级代号字符打印出来。
c++编程:编写一个函数,按所给的百分制的成绩分数,返回与该分数对应的等级代号字符
好的,根据您的要求,我编写了以下C++函数,可以实现您所需的功能:
```c++
char getGrade(int score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else if (score >= 60) {
return 'D';
} else {
return 'E';
}
}
```
以上代码中,`getGrade`函数接收一个整数参数`score`,表示百分制的成绩分数。根据该分数所在的范围,返回相应的等级代号字符。
例如,如果您调用`getGrade(85)`,函数将返回字符`B`,代表该分数对应的等级为B级。
希望这个函数能够满足您的需求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)