Coverage for e2xgrader/graders/multiplechoice.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-03-14 13:22 +0100

1from logging import Logger 

2from typing import Optional, Tuple 

3 

4from nbformat.notebooknode import NotebookNode 

5 

6from ..utils.extra_cells import get_choices, get_instructor_choices, get_num_of_choices 

7from .base import BaseGrader 

8 

9 

10class MultipleChoiceGrader(BaseGrader): 

11 def determine_grade( 

12 self, cell: NotebookNode, log: Logger = None 

13 ) -> Tuple[Optional[float], float]: 

14 max_points = float(cell.metadata["nbgrader"]["points"]) 

15 student_choices = get_choices(cell) 

16 instructor_choices = get_instructor_choices(cell) 

17 option_points = max_points / get_num_of_choices(cell) 

18 

19 points = 0 

20 

21 for i in range(get_num_of_choices(cell)): 

22 if ((i in student_choices) and (i in instructor_choices)) or ( 

23 (i not in student_choices) and (i not in instructor_choices) 

24 ): 

25 points += option_points 

26 else: 

27 points -= option_points 

28 

29 return max(0, round(points, 1)), max_points