Write a program that will auto-grade several multiple-choice exams from a list o
ID: 3578384 • Letter: W
Question
Write a program that will auto-grade several multiple-choice exams from a list of students.
This program will also provide some statistic information about this list of students/exams. The exam has several
questions, each answered with a letter in the range of ‘A’ or ‘a’ through ‘F’ or ‘f’.
The student answers are all stored in a text file as show below. Each line corresponds to one student and contains:
a student ID followed by, the first name, last name, and a string of characters representing the answers of that
student. The student ID, first name, last name, and answers are separated by one or more spaces. Note that your
program should handle ANY data, lower or upper case.
The program also uses another file named “answers.txt” as input. The user should be prompt to input the name of
the file. This file contains the following information:
• Number of questions in the exam in the first line of the file “answers.txt”.
• The answer key to be used for grading in the second line.
• The name of the file that holds the students exams/answers, as in our example.
Program should work for ANY data that respects this specification.
For simplicity assume that each student answers all the questions. If a student answers a questions with a
letter other than A-E or a-e, then the question is considered wrong.
Program should also compute a percentile score for each student by comparing the answers for that student
to the answers key (given in the answers.txt file), and a curved grade in ‘A’ through ‘F’ based on the following:
• >=90 A
• <90 and >=80 B
• <80 and >= 70 C
• <70 and >= 60 D
• <60 and >= 50 E
• <50 F
The program generates, as a result, a file called (a)“classgrades.txt” and (b)“statistics.txt:
a) In each line the following information must be there: student ID, first name, last name, score (out of 100),
and letter grade of the student. If you have partial score, your students should also have.
b) In this output file we also want to know the statistics of this class. So you are required to present:
number of processed students, average grade, average score, lowest score and highest score (with
its respective grade/letter for each one of these). It is also required that, for the lowest/highest score the
name of the student is also obtained This should be presented in the screen as well.
Important information:
• The user must input the name of the files containing the “answers”. In this file, there will be the
name for the other file with all students’ information.
• Working with 12 students, but your code should be able to handle any number of students.
• It is not mandatory to use array or structs.
• Functions need to make use of parameters/typed, so don’t create all functions void with no
parameters.
• All functions, no exception, need to use headers.
• Comments in the code are expected.
• Indentation and organization are expected. Use variable names that make sense, organize
code.
• You are REQUIRED to structure your program into functions. You need to define AT LEAST 6
functions besides the main() function, you can do more.
• Average/Percentile can have decimal points.
Explanation / Answer
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 13 17:20:53 2016
@author: naresh
"""
import os
os.chdir('/home/naresh/Desktop/chegg/')
fname = input("Enter a filename where the answers are stored: ").lower()
dict1 = {}
def read_file(fname):
with open(fname) as f:
ans_l = [line.rstrip(' ') for line in f]
no_of_ans = ans_l[0]
ans_grade = ans_l[1]
std_fname = ans_l[2]
calculation(no_of_ans,ans_grade,read_stddet(std_fname))
def read_stddet(std_fname):
with open(std_fname) as f:
std_l = [line.rstrip(' ') for line in f]
return std_l
def calculation(ansrcount,grades,stdlist):
student_det = []
if len(grades) < 2*int(ansrcount):
grades = grades.replace(" ", "")
ans_grades = list(grades.upper())
for val in stdlist:
student_det.append(val.split(" "))
for i in range(1,int(ansrcount)+1):
if i not in dict1:
dict1[i] = ans_grades[i-1]
calculate_score(dict1,student_det,ansrcount)
def calculate_score(dict1,student_det,ansrcount):
student_scores = []
for val in student_det:
sid = val[0]
fname = val[1]
lname = val[2]
s_grades = val[3:13]
for i in range(len(s_grades)):
s_grades[i] = s_grades[i].upper()
count = 0
for i in range(1,int(ansrcount)+1):
if s_grades[i-1] == dict1[i]:
count += 1
student_scores.append([sid,fname,lname,str(int((count/int(ansrcount))*100))])
calculate_percentile(student_scores,ansrcount)
def calculate_percentile(student_scores,ansrcount):
student_grades = []
for val in student_scores:
sid = val[0]
fname = val[1]
lname = val[2]
score = int(val[3])
if score >= 90:
student_grades.append([sid,fname,lname,'A'])
elif score >= 80 and score < 90:
student_grades.append([sid,fname,lname,'B'])
elif score >= 70 and score < 80:
student_grades.append([sid,fname,lname,'C'])
elif score >= 60 and score < 70:
student_grades.append([sid,fname,lname,'D'])
elif score >= 50 and score < 60:
student_grades.append([sid,fname,lname,'E'])
elif score < 50:
student_grades.append([sid,fname,lname,'F'])
fileop(student_scores,student_grades)
def fileop(student_scores,student_grades):
student_scores.sort(key=lambda x: x[3])
with open('classgrades.txt', mode='wt', encoding='utf-8') as myfile:
for val in student_scores:
myfile.write(' ')
for data in val:
myfile.write(data+' ')
with open('statistics.txt', mode='wt', encoding='utf-8') as myfile:
myfile.write("No.of students processed" + ' ' + str(len(student_scores)) )
myfile.write(" ")
score = 0
for val in student_scores:
score += int(val[3])
myfile.write("Average Score" + ' ' + str(round((score/len(student_scores)),2)))
myfile.write(" ")
score = int((score/len(student_scores)))
if score >= 90:
myfile.write("Average Grade" + ' ' + 'A')
elif score >= 80 and score < 90:
myfile.write("Average Grade" + ' ' + 'B')
elif score >= 70 and score < 80:
myfile.write("Average Grade" + ' ' + 'C')
elif score >= 60 and score < 70:
myfile.write("Average Grade" + ' ' + 'D')
elif score >= 50 and score < 60:
myfile.write("Average Grade" + ' ' + 'E')
elif score < 50:
myfile.write("Average Grade" + ' ' + 'F')
myfile.write(" ")
myfile.write("Name:" + ' ' + student_scores[0][1] + ' ' + "Lowest Score" + ' ' + student_scores[0][3])
myfile.write(" ")
myfile.write("Name:" + ' ' + student_scores[-1][1] + ' ' + "Highest Score" + ' ' + student_scores[-1][3])
read_file(fname)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.