Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Score Filer takes the scores of a test and returns the Average, Lowest, and Highest score(s) of the test.
# Input student records from a file and return a list of records:
def read(fileName):
listOfStudents = [];
inFile = open(fileName, 'rU'); # read in "universal end-of-line" mode
for line in inFile:
# Strip unnecessary '\n' from right end:
line = line.rstrip('\n');
student = line.split(',');
# student = ['last-name', 'first-name', 'score']
listOfStudents.append(student);
inFile.close();
return listOfStudents;
# Output a list of student records into a file :
def write(listOfStudents, fileName):
outFile = open(fileName, 'w');
for student in listOfStudents:
string = ','.join(student);
print >> outFile, string;
outFile.close();
# Input student records, add letter grades, and output records:
def scores():
inFileName = raw_input('Enter name of file to read from: ');
listOfStudents = read(inFileName);
listOfStudents.sort();
for student in listOfStudents:
# student = ['last-name', 'first-name', 'score']
score = int(student[2]); # Convert score string into int score
student.append(str(theGrade(score)));
outFileName = raw_input('Enter name of file to write into: ');
write(listOfStudents, outFileName);
print 'Students processed:', len(listOfStudents);
# Transform score into a letter grade:
def theGrade(score):
if (score < 60): grade = 'F';
elif (score < 70): grade = 'D';
elif (score < 80): grade = 'C';
elif (score < 90): grade = 'B';
else: grade = 'A';
return grade;
# Input student records from a file; determine and output min, max, and average scores:
def summary():
inFileName = raw_input('Enter name of file to read from: ');
listOfStudents = read(inFileName);
counter = len(listOfStudents);
print 'Students processed:', counter;
if (counter > 0):
total = 0;
firstStudent = listOfStudents[0];
firstScore = firstStudent[2];
minScore = int(firstScore);
maxScore = int(firstScore);
for student in listOfStudents:
score = int(student[2]);
total = total + score;
if (score < minScore): minScore = score;
if (maxScore < score): maxScore = score;
print 'Average score:', round(total / float(counter));
print 'Lowest score:', minScore;
print 'Highest score:', maxScore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment