There is a midterm.csv file about the grading result of a course's midterm exam. The first row presents the header. Then, each row presents a student's enrolled name and his or her score. Write a program to read the file and collect all the scores in a list (without header row!) Download the midterm.csv file. Classify those scores according to the criteria given by question 2 in Problem Set 1. Then, draw a histogram with plotly for ranks A, B, C, D, F.
时间: 2024-02-18 08:06:09 浏览: 136
Sure, here's the Python code to read the midterm.csv file and collect the scores in a list:
```python
import csv
scores = []
with open('midterm.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # skip header row
for row in reader:
scores.append(int(row[1]))
print(scores)
```
To classify the scores based on the criteria given in question 2 of Problem Set 1, we can write a function like this:
```python
def classify_scores(scores):
a_scores = []
b_scores = []
c_scores = []
d_scores = []
f_scores = []
for score in scores:
if score >= 90:
a_scores.append(score)
elif score >= 80:
b_scores.append(score)
elif score >= 70:
c_scores.append(score)
elif score >= 60:
d_scores.append(score)
else:
f_scores.append(score)
return (a_scores, b_scores, c_scores, d_scores, f_scores)
```
Finally, we can use Plotly to draw a histogram for the classified scores:
```python
import plotly.graph_objects as go
(a_scores, b_scores, c_scores, d_scores, f_scores) = classify_scores(scores)
fig = go.Figure()
fig.add_trace(go.Histogram(x=a_scores, name='A'))
fig.add_trace(go.Histogram(x=b_scores, name='B'))
fig.add_trace(go.Histogram(x=c_scores, name='C'))
fig.add_trace(go.Histogram(x=d_scores, name='D'))
fig.add_trace(go.Histogram(x=f_scores, name='F'))
fig.update_layout(
title='Histogram of Midterm Exam Scores',
xaxis_title='Score',
yaxis_title='Count',
barmode='stack'
)
fig.show()
```
This should produce a histogram with 5 bars representing the counts of scores in each rank (A, B, C, D, F).
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](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)