/ / Durchschnitt der Quizdaten mit Python ermitteln (Fehlerbehebung) - Python, Wörterbuch

Durchschnitt der Quizdaten mit Python (Problembehandlung) - Python, Wörterbuch

Ich muss die Ergebnisse aus einer Datei mit Quizfragen für Schüler einlesen und die höchste Punktzahl aus den drei Versuchen zurückgeben. Ich muss Hash / Dictionary in Python verwenden. Folgendes habe ich bisher.

STD_ID  = {}
ATTEMPTS = {}
SCORE= {}

f= open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
line = line.rstrip()
ents = line.split("t")
did = ents[1]
if did in STD_ID:
ATTEMPTS[did] += 3
SCORE[did] += int(ents[2])
else:
STD_ID[did]  = ents[2]
ATTEMPTS[did] = 3
SCORE[did] = int(ents[2])


for  key in STD_ID:
print("Avg score for Student", key, "=",SCORE)

Textdaten in Datei.

FILE
STD_ID  ATT_NUM SCORE
S23Y    1   85
S03X    1   80
S34Z    1   19
S54M    1   23
S34Z    2   25
S01X    1   79
S03X    2   10
S23Y    2   09
S34Z    3   92
S54M    2   96
S23Y    3   74
S54M    3   65
S03X    3   54

Meine Ergebnisse sind wie folgt:

Avg score for Student 1 = {"1": 286, "2": 140, "3": 285}
Avg score for Student 2 = {"1": 286, "2": 140, "3": 285}
Avg score for Student 3 = {"1": 286, "2": 140, "3": 285}

Antworten:

0 für die Antwort № 1

Wenn Sie die Punktzahl nicht für jeden Versuch speichern müssen, warum ersetzen Sie dann nicht einfach den Wert, wenn er höher ist als der aktuell aufgezeichnete Versuch?

students = {}

f = open("std_attempt_score.txt", "r")
line = f.readline()
for line in f:
line = line.rstrip()
ents = line.split(",")
student_id = ents[0]
attempt_score = int(ents[2])

score = students.setdefault(student_id, attempt_score)

if attempt_score > score:
students[student_id] = attempt_score


for student_id, score in students.items():
print("Highest score for Student", student_id, "=", score)

0 für die Antwort № 2

Probleme mit dem angegebenen Code:

Die Schleife beginnt mit einem Textheader, der nicht geprüft wird, bevor die Anweisung if else für student_id geprüft wird SCORE am ende von code.no wird der durchschnitt in schrift genommen. </ p></ p>

Fester Code

Hier ist ein Beispielcode </ p>
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]), "nn")

Beispielausgabe:

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