<p><strong>给定代码的问题:</strong></p>
循环以一个文本头开始,在转到if else语句进行学生id检查之前不检查该文本头代码编号在脚本中取平均值。
<p><strong>固定代码</p>
下面是一个示例代码
<pre><code>STD_ID = {}
ATTEMPTS = {}
SCORE= {}
f= open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
line = line.rstrip()
entss = " ".join(line.split())
ents = entss.split(" ")
did = ents[0]
if not line.startswith("STD_ID"): # check for the header you definitely want to skip this line
if did in STD_ID :
ATTEMPTS[did] += 1
SCORE[did].append(int(ents[2])) #polulate student_id with marks
else:
STD_ID[did] = [ents[0]] #Start Dictionary with student ID and marks
ATTEMPTS[did] = 1
SCORE[did] = [int(ents[2])]
for key in sorted(STD_ID):
if len(SCORE[key]) < 3:
dumValues = [0] * (3 - len(SCORE[key]))
SCORE[key] = SCORE[key] + dumValues # add 0's in un-attempted quizzes.
print("Student ID {0} Score Summary : \n".format(key))
print("Top 3 Quiz : ", sorted(SCORE[key], reverse=True)[:3])
print("Avg score of top 3 quiz : " , sum(sorted(SCORE[key], reverse=True)[:3]) / 3)
print("Quiz With Highest Marks out of 3 Top Quizzes : ", sorted(SCORE[key], reverse=True)[0])
print("Total Marks in 3 Attempts : ", sum(sorted(SCORE[key], reverse=True)[:3]), "\n\n")
</code></pre>
<p><strong>样本输出:</strong></p>
<pre><code>Student ID S01X Score Summary :
Top 3 Quiz : [79, 0, 0]
Avg score of top 3 quiz : 26.333333333333332
Quiz With Highest Marks out of 3 Top Quizzes : 79
Total Marks in 3 Attempts : 79
Student ID S03X Score Summary :
Top 3 Quiz : [80, 54, 10]
Avg score of top 3 quiz : 48.0
Quiz With Highest Marks out of 3 Top Quizzes : 80
Total Marks in 3 Attempts : 144
Student ID S23Y Score Summary :
Top 3 Quiz : [85, 74, 9]
Avg score of top 3 quiz : 56.0
Quiz With Highest Marks out of 3 Top Quizzes : 85
Total Marks in 3 Attempts : 168
Student ID S34Z Score Summary :
Top 3 Quiz : [92, 25, 19]
Avg score of top 3 quiz : 45.333333333333336
Quiz With Highest Marks out of 3 Top Quizzes : 92
Total Marks in 3 Attempts : 136
Student ID S54M Score Summary :
Top 3 Quiz : [96, 65, 23]
Avg score of top 3 quiz : 61.333333333333336
Quiz With Highest Marks out of 3 Top Quizzes : 96
Total Marks in 3 Attempts : 184
</code></pre>