0

i want to compare a student's answer to the model answer. the student undertakes a test of multiple choice questions. there is a total of 5 questions and each question has 3 multiple choices. the student chooses the following choices for all questions: "12231". e.g: for Q(1): student picks option "1",for Q(2): student picks option "2"...etc. now, i need to calculate the student's total mark by comparing it with the Model answer: "12132". This student received a mark of 3/5. The student's answer will always be as the same length as the model one. e.g. the student wont skip any questions.

i need to do the exact same thing, but with hundreds of students. can i do it using a code? i can only think of using a for loop and iterating the students answer, but i cannot think of a way to compare both together and calculate the student's mark.

2 Answers 2

1

Any time you have a question that ends with "in another ___ at the same position", the answer is almost always zip.

If you zip together two strings, like the student's answers and the answer key, you get an iterable of pairs: the student's first answer and the answer key's first answer, then the two second answers, and so on.

So, if you want to count how many answers a student got right, you just loop over the zip, using a for statement or comprehension. For example:

score = sum(student==correct for student, correct in zip(student_answers, answer_key))

This uses a little extra trick: if you sum up a bunch of Boolean values, the True values count as 1, and the False values as 0. But otherwise, there's nothing more to than looping over zip.

And if you want to do this for each student's answers in a list of students, that's just another loop around this one. For example:

all_student_scores = []
for student_answers in all_student_answers:
    score = sum(student==correct for student, correct in zip(student_answers, answer_key)
    all_student_scores.append(score)

Or, if you want to be super-concise:

all_student_scores = [sum(student==correct for student, correct in zip(student_answers, answer_key)
                      for student_answers in all_student_answers]
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the distance package - it provides a hamming distance calculator:

import distance
distance.hamming("12231", "22131")

Now, if you have a list of student answers (str) and the model, you can do:

def score(student_answers,model):
    return len(model)-[distance.hamming(ans,model) for ans in student_answers]

2 Comments

thanks, but unfortunately we're not allowed to import anything.
@Moh'dH in this case, use abarnert's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.