How to Create a Grader in Python with JSON

How to Create a Grader in Python with JSON



Hey everyone :wave:! Today I will show you how to make a grader that you can pre-populate with random values or populate by hand to figure out letter and percentage grades from fractional grades.

Note: We will use Python to code and JSON to store our values for you to collect.


grader.py


First create a Python file and name it grader.py and put into import json.
To be able to easily create an our students with seperate values we will use a class.

class Grader:

We then need to create a __init__ function to be able to add parameters to our Grader class.

def __init__(self, firstName, lastName, subject_teacher_grades):
    self.firstName = firstName
    self.lastName = lastName
    self.subject_teacher_grades = subject_teacher_grades

Now to calculate the students grades add this function to Grader.

def calculate_individual_grades(self):
    individual_grades = {}
    for subject, teacher_grade in self.subject_teacher_grades.items():
        teacher, grade = teacher_grade.split(': ')
        individual_grades[subject] = grade
    return individual_grades

This will remove the teachers from the inputted values when you initialize a student.

Now you can add this: self.individual_grades = self.calculate_individual_grades() to __init__.

This will make it so our function up above will run

We can also convert our fraction grades into percentages like so:

def calculate_percentage(self, grade):
    return (int(grade.split('/')[0]) / 10) * 100

Make sure to add that code to the Grader class.

Now to add the letter grades functionality we can add this function to Grader:

def calculate_letter_grades(self):
    letter_grades = {}
    for subject, grade in self.individual_grades.items():
        percentage = self.calculate_percentage(grade)
        if percentage >= 90:
            letter_grades[subject] = 'A'
        elif percentage >= 80:
            letter_grades[subject] = 'B'
        elif percentage >= 70:
            letter_grades[subject] = 'C'
        elif percentage >= 60:
            letter_grades[subject] = 'D'
        else:
            letter_grades[subject] = 'F'
    return letter_grades

This code checks the percentages from earlier and finds out the letter grade the student got for each subject

So now in __str__ we can get our letter grades: self.letter_grades = self.calculate_letter_grades()

We can now add the JSON part to save student values:

with open('students.json', 'a') as json_file:
    json.dump(students, json_file, indent=4)
    json_file.write('\n')  # Add a newline for multiple students

Note: You will have to create a file called students.json to allow the JSON saving to work

We have all of the grader functionality done now we have to add something for when we want to print out our data:

def __str__(self):
    return f"{self.firstName} {self.lastName}\n\n\n" \
           f"Subject Teacher Grades: {self.subject_teacher_grades}\n\n" \
           f"Individual Grades: {self.individual_grades}\n\n" \
           f"Percentage Grades: {self.percentage_grades}\n\n" \
           f"Letter Grades: {self.letter_grades}\n\n"

In a class __str__ allows you to change what gets printed when you call the class

Make sure to add this to Grader.

We are now finished with grader.py time to move on to main.py!


main.py


First off add this to `main.py` so we can use the grader: `from grader import Grader `

Now if you want to populate the data by hand you can do something like this:

JohnDoe = Grader("John", "Doe",  {'Math': 'Mr.Smith': '5/15', 'Science': 'Mrs.Brown': '5/15'})

And to see that data you then can do this:

print(JohnDoe)

You will see this when the project it ran:

John Doe

Subject Teacher Grades:  {'Math': 'Mr.Smith: 5/15', 'Science': 'Mrs.Brown: 5/15'}

Individual Grades: {'Math': '5/15', 'Science': '5/15'}

Percentage Grades: {'Math': '33.333', 'Science': '33.333'}

Letter Grades: {'Math': 'F', 'Science': 'F'}

If you want to randomly populate the data then you can use this:

from grader import Grader
import random

first_names = ['John', 'Jane', 'Michael', 'Emily', 'James', 'Olivia', 'David', 'Emma', 'William', 'Sophia']
last_names = ['Smith', 'Johnson', 'Brown', 'Taylor', 'Miller', 'Anderson', 'Martinez', 'Harris', 'Jackson', 'Davis']
subjects = ['Math', 'Science', 'ELA', 'Social Studies']
teachers = ['Mr. Smith', 'Mrs. Johnson', 'Mr. Davis', 'Ms. Brown', 'Dr. Taylor']

for _ in range(5): # 5 is number of students change it to get more or less data
    first_name = random.choice(first_names)
    last_name = random.choice(last_names)
    
    subject_teacher_grades = {}
    
    for subject in subjects:
        teacher = random.choice(teachers)
        grade = f'{random.randint(0, 15)}/15' if subject == 'Math' else f'{random.randint(0, 10)}/10'
        subject_teacher_grades[subject] = f'{teacher}: {grade}'

    grader = Grader(first_name, last_name, subject_teacher_grades)
    print(grader)

This picks random values to set as our data when calling our class

Now we are all done with our grader!


Full code if you have any problems or want to check it out: Grading System with JSON

I hope you all enjoyed this tutorial and found it helpful!

Rate how good/helpful the tutorial was (1 Being Terrible - 10 Being Really Good/Helpful)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
0 voters

(If you didn’t think it was good leave a reply saying why so I can improve it and my future tutorials)

Made with :heart: by @SalladShooter

6 Likes