Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.31/5 (4 votes)
Modify the code in this program to include following:

a. refactor the script into a function, get_grades(),which returns a dictionary containing
the grade (number of correct answers) achieved per student;

b. write a new function, sorted_by_grade(), which displays students in ascending order of
the grade they achieved; and

c. write a new function, main(), which simply calls the functions you defined in a and b above


<pre>student_answers = {
    "Adam": ['A', 'B', 'A', 'C', 'C', 'D', 'A', 'D', 'A', 'E'],
    "Bob":  ['D', 'B', 'A', 'B', 'C', 'A', 'C', 'D', 'A', 'E'],
    "Carol":['A', 'C', 'D', 'A', 'C', 'B', 'C', 'D', 'A', 'E'],
    "Jon":  ['C', 'B', 'A', 'E', 'D', 'C', 'A', 'D', 'A', 'E'],
    "Liz":  ['A', 'B', 'A', 'C', 'A', 'E', 'D', 'E', 'A', 'E'],
    "Marc": ['A', 'C', 'D', 'C', 'C', 'D', 'A', 'D', 'A', 'E'],
    "Mia":  ['B', 'B', 'E', 'C', 'A', 'D', 'A', 'D', 'A', 'E'],
    "Zia":  ['E', 'B', 'E', 'C', 'A', 'E', 'C', 'E', 'A', 'E']
}

# Key to the questions
model_answers = ['A', 'C', 'D', 'C', 'A', 'D', 'A', 'D', 'A', 'E']

# Grade all answers
for name in student_answers:
    # Grade for one student
    correctCount = 0
    for j in range(len(student_answers[name])):
        if student_answers[name][j] == model_answers[j]:
            correctCount += 1
    print(name, "achieved", correctCount, "correct answers.")</pr


What I have tried:

I have done this but as you can see it above i am not able to do it Please help me to do this code.
Posted
Updated 7-Nov-21 7:31am
v5
Comments
Tony Hill 7-Nov-21 14:13pm    
Why did you remove the question you asked, don't you think that leaving the question there might help somebody else and adds context to the solution.
WillDavid 7-Nov-21 14:49pm    
Ohh, Ok i will post as soon as Possible

1 solution

Start by looking closely at each question.
a. refactor the script into a function, get_grades() - so you need to create a function which returns a dictionary:
Python
def get_grades():
    grades = {} # declare an empty dictionary
    # add the code here to build the dictionary entries
    return grades

b. write a new function, sorted_by_grade()
Python
def sorted_by_grade(grades):
    # use the grades dictionary to create a list of students sorted by grade, and display the details

c. write a new function, main()
Python
def main():
    results = get_grades()
    sorted_by_grade(results)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900