成绩统计python代码
时间: 2023-11-16 16:58:13 浏览: 136
这段Python代码是用来统计学生考试成绩的。首先,通过输入一个整数n,表示学生人数,然后通过循环n次,将每个学生的成绩添加到一个列表中。接着,通过遍历列表,统计及格人数和优秀人数,并计算及格率和优秀率。最后,使用print函数输出结果,其中使用了f-string格式化字符串,round函数进行四舍五入和保留小数位数。
具体代码如下:
```
import os
import sys
n = int(input()) #将输入的数据转化为整型,因为默认是字符串类型
n_list = list() #定义一个空列表
for i in range(n): #生成1到n-1,步长为1的列表
n_list.append(int(input())) #尾加元素到列表中
a = 0 #及格人数
b = 0 #优秀人数
for x in n_list:
if x >= 60:
a += 1
if x >= 85:
b += 1
h = a / n * 100 #及格率
k = b / n * 100 #优秀率
print(f"{round(h)}%") #round四舍五入:round(2.3)->2
print(f"{round(k)}%") #一位小数:round(2.45,1)->2.5 #两位小数:round(2.675,1)->2.67
```
相关问题
Python成绩统计代码
Python成绩统计代码可以用于计算一个班级的成绩情况,包括总分、平均分、最高分、最低分等。一般情况下,这种代码的实现需要用到列表、循环和条件语句等基础知识。
下面是一个简单的Python成绩统计代码示例:
```
# 定义一个列表,存放学生成绩
scores = [85, 76, 92, 88, 79, 91, 90, 77, 84, 80]
# 计算总分和平均分
total = sum(scores)
average = total / len(scores)
# 计算最高分和最低分
max_score = max(scores)
min_score = min(scores)
# 输出统计结果
print("总分:", total)
print("平均分:", average)
print("最高分:", max_score)
print("最低分:", min_score)
```
成绩统计python
成绩统计是指对一组学生的成绩进行整理、计算和分析的过程。在Python中,可以使用列表或字典等数据结构来存储学生的成绩信息,并通过编写相应的函数来实现成绩统计功能。
以下是一个简单的示例,演示如何使用Python进行成绩统计:
```python
# 定义一个包含学生成绩的字典
scores = {
'张三': [80, 75, 90],
'李四': [85, 90, 92],
'王五': [70, 80, 75],
'赵六': [95, 88, 92]
}
# 计算每个学生的平均成绩
def calculate_average(scores):
averages = {}
for student, score_list in scores.items():
average = sum(score_list) / len(score_list)
averages[student] = average
return averages
# 计算班级的平均成绩
def calculate_class_average(scores):
total_scores = []
for score_list in scores.values():
total_scores.extend(score_list)
class_average = sum(total_scores) / len(total_scores)
return class_average
# 调用函数进行成绩统计
student_averages = calculate_average(scores)
class_average = calculate_class_average(scores)
# 打印每个学生的平均成绩
for student, average in student_averages.items():
print(f'{student}的平均成绩为:{average}')
# 打印班级的平均成绩
print(f'班级的平均成绩为:{class_average}')
```
这段代码中,我们首先定义了一个包含学生成绩的字典`scores`,其中键表示学生姓名,值表示学生的成绩列表。然后,我们编写了两个函数`calculate_average`和`calculate_class_average`来计算学生的平均成绩和班级的平均成绩。最后,我们调用这两个函数并打印结果。
相关推荐
![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)