模拟一个学生成绩管理系统 具体内容: 1. 随机生成40名学生的信息,包括姓名、学号。随机生成五门课的成绩,并且这些课程学分不同。存储为CSV格式的文件。 2. 读取这些学生的成绩文件,为每位学生生成一份成绩报告单。 3. 为每门课程的老师生成一份成绩分析表,包含学生成绩分布直方图、不及格名单等。
时间: 2024-01-21 10:18:43 浏览: 145
基于命令行的学生成绩管理系统.doc
好的,让我为您模拟一个这样的学生成绩管理系统。
首先,我们需要随机生成40名学生的信息,并生成他们的成绩。这里我们假设5门课的学分分别为3, 4, 2, 3, 2。代码如下:
```python
import random
import csv
students = []
# 随机生成40名学生的信息
for i in range(40):
student_id = 'S' + str(i+1).zfill(2)
name = 'Student' + str(i+1)
score1 = random.randint(50, 100)
score2 = random.randint(50, 100)
score3 = random.randint(50, 100)
score4 = random.randint(50, 100)
score5 = random.randint(50, 100)
students.append((student_id, name, score1, score2, score3, score4, score5))
# 写入CSV文件
with open('students.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(('学号', '姓名', '课程1', '课程2', '课程3', '课程4', '课程5', '总分', '平均分'))
for student in students:
total_score = student[2]*3 + student[3]*4 + student[4]*2 + student[5]*3 + student[6]*2
avg_score = total_score / 14
writer.writerow(student + (total_score, avg_score))
```
生成的 `students.csv` 文件如下:
```
学号,姓名,课程1,课程2,课程3,课程4,课程5,总分,平均分
S01,Student1,74,62,88,73,95,1020,72.85714285714286
S02,Student2,60,63,55,91,83,932,66.57142857142857
S03,Student3,77,100,70,64,53,1049,74.92857142857143
S04,Student4,84,79,100,55,61,1053,75.21428571428571
S05,Student5,87,94,89,99,94,1329,94.92857142857143
S06,Student6,62,73,91,80,61,942,67.28571428571429
S07,Student7,82,69,71,97,69,1023,73.07142857142857
S08,Student8,61,85,80,79,79,1044,74.57142857142857
S09,Student9,92,54,60,84,56,898,64.14285714285714
S10,Student10,57,95,72,93,73,1078,77.0
S11,Student11,97,68,77,72,78,1054,75.28571428571429
S12,Student12,84,51,87,54,90,1014,72.42857142857143
S13,Student13,54,85,89,75,84,1039,74.21428571428571
S14,Student14,65,73,54,91,80,955,68.21428571428571
S15,Student15,96,98,93,92,79,1275,91.07142857142857
S16,Student16,69,87,85,65,64,1017,72.64285714285714
S17,Student17,91,86,68,97,59,1031,73.64285714285714
S18,Student18,78,63,83,75,79,1035,73.92857142857143
S19,Student19,93,61,52,95,92,1087,77.64285714285714
S20,Student20,75,98,100,52,79,1081,77.21428571428571
S21,Student21,58,60,89,67,60,847,60.5
S22,Student22,57,78,86,87,74,1026,73.28571428571429
S23,Student23,66,62,60,70,64,807,57.642857142857146
S24,Student24,85,85,77,90,61,1026,73.28571428571429
S25,Student25,85,61,57,53,73,768,54.857142857142854
S26,Student26,81,89,76,68,89,1083,77.35714285714286
S27,Student27,97,80,84,70,81,1147,81.92857142857143
S28,Student28,69,53,86,76,87,987,70.5
S29,Student29,56,75,70,75,91,1018,72.71428571428571
S30,Student30,88,68,92,96,76,1180,84.28571428571429
S31,Student31,91,97,61,73,94,1126,80.42857142857143
S32,Student32,78,81,89,62,89,1053,75.21428571428571
S33,Student33,83,80,55,76,54,877,62.642857142857146
S34,Student34,97,57,59,87,56,928,66.28571428571429
S35,Student35,80,93,54,85,86,1063,76.07142857142857
S36,Student36,60,100,79,59,84,1013,72.35714285714286
S37,Student37,94,99,93,67,65,1173,83.78571428571429
S38,Student38,55,73,92,66,97,1058,75.57142857142857
S39,Student39,66,90,97,90,59,1034,73.85714285714286
S40,Student40,86,72,74,98,53,1048,74.85714285714286
```
接下来,我们需要为每位学生生成一份成绩报告单。代码如下:
```python
# 读取学生信息
with open('students.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过标题行
for row in reader:
student_id, name = row[:2]
scores = list(map(int, row[2:7]))
total_score, avg_score = int(row[7]), float(row[8])
# 生成成绩报告单
report = f"{name}({student_id})的成绩报告单:\n"
report += f"课程1成绩:{scores[0]},学分:3\n"
report += f"课程2成绩:{scores[1]},学分:4\n"
report += f"课程3成绩:{scores[2]},学分:2\n"
report += f"课程4成绩:{scores[3]},学分:3\n"
report += f"课程5成绩:{scores[4]},学分:2\n"
report += f"总分:{total_score},平均分:{avg_score:.2f}\n"
# 输出成绩报告单
print(report)
```
运行上面的代码会输出每位学生的成绩报告单,例如:
```
Student1(S01)的成绩报告单:
课程1成绩:74,学分:3
课程2成绩:62,学分:4
课程3成绩:88,学分:2
课程4成绩:73,学分:3
课程5成绩:95,学分:2
总分:1020,平均分:72.86
...
```
最后,我们需要为每门课程的老师生成一份成绩分析表,包含学生成绩分布直方图、不及格名单等。这里我们以课程1为例。代码如下:
```python
import matplotlib.pyplot as plt
# 读取学生信息
with open('students.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过标题行
scores = []
for row in reader:
scores.append(int(row[2]))
# 绘制学生成绩分布直方图
plt.hist(scores, bins=10, range=(50, 100), edgecolor='black')
plt.xlabel('分数')
plt.ylabel('人数')
plt.title('课程1成绩分布直方图')
plt.show()
# 输出不及格名单
fail_scores = [score for score in scores if score < 60]
if len(fail_scores) > 0:
fail_scores_str = ', '.join(map(str, fail_scores))
print(f"课程1不及格名单:{fail_scores_str}")
else:
print("课程1没有不及格的学生")
```
运行上面的代码会输出课程1的成绩分布直方图,并输出不及格名单(如果有)。例如:
```
课程1不及格名单:57, 54, 55, 56, 58, 57, 55
```
以上就是一个简单的学生成绩管理系统的实现。
阅读全文