Question

----------------------------------------------------------- #Recall in an earlier problem you were given two lists: one #list was a student's...

-----------------------------------------------------------
#Recall in an earlier problem you were given two lists: one
#list was a student's answers to a test, and the other was
#the answer key. Your goal was to return a score
#representing the number of problems the student got correct.
#
#Let's do that again, but with dictionaries instead of lists.
#Write a function called calculate_score. calculate_score
#should take two parameters: a dictionary of student answers
#and a dictionary of correct answers. Both dictionaries should
#have integers as their keys, and one-character strings as
#their values.
#
#calculate_score should count how many questions the student
#got right. Or, in more precise terms, calculate_score should
#count how many keys for which the associated value is the
#same in the student's dictionary and in the answer key
#dictionary.
#
#As before, it is possible that there will be more answers in
#one than the other. This means that these answers don't
#belong to the same test! If that happens, return -1.


#Add your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: 3
student_answers = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E"}
correct_answers = {1: "A", 2: "B", 3: "A", 4: "D", 5: "B"}
print(calculate_score(student_answers, correct_answers))


0 0
Add a comment Improve this question Transcribed image text
Answer #1
# Recall in an earlier problem you were given two lists: one
# list was a student's answers to a test, and the other was
# the answer key. Your goal was to return a score
# representing the number of problems the student got correct.
#
# Let's do that again, but with dictionaries instead of lists.
# Write a function called calculate_score. calculate_score
# should take two parameters: a dictionary of student answers
# and a dictionary of correct answers. Both dictionaries should
# have integers as their keys, and one-character strings as
# their values.
#
# calculate_score should count how many questions the student
# got right. Or, in more precise terms, calculate_score should
# count how many keys for which the associated value is the
# same in the student's dictionary and in the answer key
# dictionary.
#
# As before, it is possible that there will be more answers in
# one than the other. This means that these answers don't
# belong to the same test! If that happens, return -1.

def calculate_score(student, correct):
    count = 0
    for k, v in student.items():
        if correct[k] == v:
            count += 1
    return count


# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: 3
student_answers = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E"}
correct_answers = {1: "A", 2: "B", 3: "A", 4: "D", 5: "B"}
print(calculate_score(student_answers, correct_answers))

Add a comment
Know the answer?
Add Answer to:
----------------------------------------------------------- #Recall in an earlier problem you were given two lists: one #list was a student's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT