Home > Enterprise >  How to compare list input to a dictionary and add point if correct
How to compare list input to a dictionary and add point if correct

Time:09-18

I am trying to create an input-based program to ask students to input their responses to each question, then compare them to the answer key[dict]. If value is matched to input, add a point.

In my current code I have already found an issue where if I input "A,B,C" as the first responses and "A" for the rest, my result says 8,9,10 are correct although they are not.

What is my error in my for loop? I would also like some recommendations on how to improve this or to change my current logic altogether.

QUANTITY_TEST_QUESTIONS = 10
ANSWERKEY = {
    1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 
    6:"e", 7:"d", 8:"c", 9:"b", 10:"a"
}

def student_response():
    responseList = []
    
    for i in range(0, QUANTITY_TEST_QUESTIONS):
        student_response = input("Enter your response: ")
        # student_response = student_response.title()
        responseList.append(student_response)
    
    print(responseList)
    return responseList


def compare_response_to_answerKey(ANSWERKEY:Dict, responseList):
    points = 0

    for k, v in ANSWERKEY.items():
        if v in responseList:
            points  = 1
            print(f"Correct Answers:{k}")

CodePudding user response:

In your compare_response_to_answerKey you only check that the answer is in the responselist, not that it corresponds with the correct answer for that question.

You may wish to zip the correct answers in the dictionary together with the response from the student, and count occurrences where they are the same.

>>> sum(1 for correct, answer in zip(ANSWERKEY.values(), student_response())
...       if answer == correct)
Enter your response: a
Enter your response: b
Enter your response: c
Enter your response: d
Enter your response: e
Enter your response: a
Enter your response: b
Enter your response: c
Enter your response: d
Enter your response: e
['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
6

We can see the effect of zipping the two together more directly with:

>>> list(zip(ANSWERKEY.values(), student_response()))
Enter your response: a
Enter your response: b
Enter your response: c
Enter your response: d
Enter your response: e
Enter your response: a
Enter your response: b
Enter your response: c
Enter your response: d
Enter your response: e
['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'), ('e', 'a'), ('d', 'b'), ('c', 'c'), ('b', 'd'), ('a', 'e')]

CodePudding user response:

I think that will work

QUANTITY_TEST_QUESTIONS = 10
ANSWERKEY = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"e", 7:"d", 8:"c", 9:"b", 10:"a"}
def student_response():
    responseList=[]
    for i in range(0, QUANTITY_TEST_QUESTIONS):
        student_response = input("Enter your response: ")
        # student_response = student_response.title()
        responseList.append(student_response)

    print(responseList)
    return responseList


def compare_response_to_answerKey(ANSWERKEY, responseList):
    points = 0

    for k, v in ANSWERKEY.items():
        if v in responseList[k-1]:
            points  = 1
            print(f"Correct Answers:{k}")




a = student_response()
compare_response_to_answerKey(ANSWERKEY, a)

Now with your input (a,b,c,a,a,a,a,a,a) work correctly.

  • Related