数据分类:录入若千名同学的姓名和成绩,把高于平均分的和低于平均分的分成两个部分然后输出。用python
时间: 2024-05-13 21:15:25 浏览: 32
可以使用Python中的列表和循环语句来实现这个任务。下面是一个简单的示例代码:
```python
# 定义一个空列表用于存储学生姓名和成绩
students = []
# 录入学生信息
while True:
name = input("请输入学生姓名:")
if not name:
break
score = float(input("请输入学生成绩:"))
students.append({'name': name, 'score': score})
# 计算平均分
total_score = 0
for student in students:
total_score += student['score']
avg_score = total_score / len(students)
# 分类输出
high_score_students = []
low_score_students = []
for student in students:
if student['score'] >= avg_score:
high_score_students.append(student)
else:
low_score_students.append(student)
print("高于平均分的学生:")
for student in high_score_students:
print(student['name'], student['score'])
print("低于平均分的学生:")
for student in low_score_students:
print(student['name'], student['score'])
```
此代码需要用户输入每个学生的姓名和成绩,然后计算平均分并将学生分类为高于平均分和低于平均分。最后,程序输出两个列表,分别包含高于平均分和低于平均分的学生姓名和成绩。
阅读全文