score=int(input()) if 90 <=score<=10: print("A") elif socre >= 80: print("B") elif socre >= 70: print("C") elif score >= 60: print("D") elif score >= 0: print("E") else: print("data error!")
时间: 2024-04-28 17:18:42 浏览: 16
这段代码是一个简单的成绩等级判断程序,它的作用是根据输入的分数判断学生的成绩等级,并输出相应的等级。
代码中使用了 if...elif...else 语句,根据分数的不同区间分别执行不同的判断条件。如果分数在 90~100 之间,则输出 A 等级;如果分数在 80~89 之间,则输出 B 等级;如果分数在 70~79 之间,则输出 C 等级;如果分数在 60~69 之间,则输出 D 等级;如果分数在 0~59 之间,则输出 E 等级;如果分数小于 0 或大于 100,则输出 data error!。
需要注意的是,代码中有一个小错误:第一行的 input() 函数中使用了错误的变量名 socre,应该改为 score。
相关问题
score = int(input()) if score < 0 or score > 100: print("成绩有误") elif score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") elif score >= 60: print("D") else: print("E")
这不是一个问题,这是一段 Python 代码,用来根据一个成绩的分数来输出对应的等级。如果成绩小于0或者大于100,输出“成绩有误”。如果成绩大于等于90,输出“A”;如果成绩大于等于80,输出“B”;如果成绩大于等于70,输出“C”;如果成绩大于等于60,输出“D”;如果成绩小于60,输出“E”。
class student(): def __init__(self,score): self.score=score def grade(self): if self.score>=90: print(4.0) elif self.score>=86: print(3.7) elif self.score>=83: print(3.3) elif self.score>=80: print(3.0) elif self.score>=76: print(2.7) elif self.score>=73: print(2.3) elif self.score>=70: print(2.0) elif self.score>=66: print(1.7) elif self.score>=63: print(1.3) elif self.score>=60: print(1.0) else: print(0) sc=int(input()) print(stu.grade())为什么这里会打印出None来!!!
你的代码中存在一个问题,即在 `print(stu.grade())` 中,你使用了 `stu` 而不是 `student`。另外,你也没有先创建一个学生对象,所以你需要先创建一个学生对象并将其分数传递给对象。修改后的代码如下:
```
class Student():
def __init__(self, score):
self.score = score
def grade(self):
if self.score >= 90:
return 4.0
elif self.score >= 86:
return 3.7
elif self.score >= 83:
return 3.3
elif self.score >= 80:
return 3.0
elif self.score >= 76:
return 2.7
elif self.score >= 73:
return 2.3
elif self.score >= 70:
return 2.0
elif self.score >= 66:
return 1.7
elif self.score >= 63:
return 1.3
elif self.score >= 60:
return 1.0
else:
return 0
score = int(input("请输入学生成绩:"))
stu = Student(score)
print(stu.grade())
```
这样就能够正确输出学生的成绩了。注意,我修改了 `grade` 方法中的 `print` 语句为 `return` 语句,并在 `print(stu.grade())` 中使用了 `return` 返回的成绩。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)