成绩统计python代码
时间: 2023-11-16 15:58:13 浏览: 244
这段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进行成绩统计:
```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`来计算学生的平均成绩和班级的平均成绩。最后,我们调用这两个函数并打印结果。
学生成绩统计代码Python
在Python中,我们可以编写一段简单的代码来统计学生成绩。这里假设我们有一个包含学生ID和成绩的数据结构,比如列表或字典。以下是一个基础示例:
```python
# 定义一个学生成绩数据结构,例如字典
students = [
{"id": 1, "score": 90},
{"id": 2, "score": 85},
{"id": 3, "score": 95},
{"id": 4, "score": 78},
# 更多学生...
]
# 统计总分、平均分以及最高分和最低分
total_score = sum(student["score"] for student in students)
average_score = total_score / len(students)
def find_max_min(scores):
max_score = max(scores, key=scores.get)
min_score = min(scores, key=scores.get)
return max_score, min_score
max_score, min_score = find_max_min({student["id"]: student["score"] for student in students})
print("总分:", total_score)
print("平均分:", average_score)
print("最高分:", max_score, "学生ID:", [id for id, score in students if score == max_score][0])
print("最低分:", min_score, "学生ID:", [id for id, score in students if score == min_score][0])
阅读全文
相关推荐














