Beginner Friendly Project: Build a Simple Student Grade Calculator With Python

Beginner Friendly Project: Build a Simple Student Grade Calculator With Python

Student grading systems are used by many schools and institutions today.

If you are a programmer, you may be contacted to build one for a client.

This article covers how to build a simple grading program with python.

Let's begin!

Step 1: Figuring out the basic feature of a grading system.

When It comes to solving a programming-related problem, it is said that understanding the problem you want to solve takes up about 25% of the job, and devising a plan to solve the problem takes another 25% of the job.

This means that about 50 - 60% of a programmer's job doesn't involve writing actual code but rather, brainstorming an approach to solve a problem.

Okay... Since many of us went through the educational system, we are aware of the problem we want to solve.

Now, how do we solve it? We need to think about the features that our grading system should have.

Our grading system should be able to extract valuable data from a collection of student's data.

Then, it should be able to process that data to give out information like each student's total score, average score, and finally a remark to tell if a student passed or fail.

This is just the basic feature that a grading system can have.

Grading systems can go from simple to complex, calculating things like the cumulative grade point average, etc. Python is very much capable of handling complex data manipulations but that is a topic for another day.

Step 2: The Execution

Let's create a dictionary containing the data of an individual student in a semester.

Since a Python dictionary consist of key: value pairs, this makes it the appropriate data structure to hold the data of a student. Let's create one:

student = {
    "maths": 10,
    "english": 30,
    "programming": 50,
    "accounting": 40
}

We can see that the student dictionary above has all the subjects the student offered and their scores respectively.

Though it is unlikely for someone to get a score of 10 in maths and a score of 50 in programming. I'm just saying... lol

Step 3: Create an empty list where we will collect and sum up all the scores from the dictionary

student_marks = []

So we have our empty list above. Now we need to collect all the scores from the student dictionary into it. How can we do that?

You probably know the answer: Python For Loops.

Step 4: Let's iterate over the dictionary and collect the scores

# the iteration
for score  in student.values():

    #the collection
    student_marks.append(score)

See something unfamiliar? Don't worry. I'll explain

Assuming you are not new to the concept of iteration and regardless, what we simply did here was to go through each subject in the student dictionary and get their respective score.

There are three different ways to loop through a dictionary. Using the .values() method is just one of them. Find out more

Then we make use of the .append() method that student_marks possess (since it is a list) to add each score as an element in it.

learn more about the .append() method - here

Combining the codes in step 1, 2, & 3 gives:

# a dictionary
student = {
    "maths": 10,
    "english": 30,
    "programming": 50,
    "accounting": 40
}

# an empty list that will contain the student's mark
student_marks = []

# the iteration
for score in student.values():

    #the collection
    student_marks.append(score)

# I don't think the student_marks list will be empty anymore
print(student_marks)

Copy, save and run the code above

This should be the output:

>>> [10, 30, 50, 40]

Not empty isn't it? Cool!

Our student_marks list has all the student's score as its element

Step 5: We sum up the scores

Its time to sum up the scores in the student_marks list to get the total score obtained by the student during the semester.

total_score = sum(student_marks)

Python has a built-in function called sum() that adds up all the elements in a list (if they are numbers).

learn more about sum() functions - here

Step 6: We calculate the student's average score

Let's call the student's average score average and calculate it as thus:

average = total_score / len(student_marks)

In basic maths, we get an average of a list of numbers by dividing the sum of the numbers by the number of numbers in the list. This is exactly what we did here.

We used the len() built-in python function which counts the number of items in a list (in this case, the number of scores which is 4)

Then we divided the sum of the scores (total_score) by the number of scores (len(student_marks))

See all Python built-in functions here and their uses

Functions and Methods and Functions and Methods. I keep shoving them in your face right? Aren't they actually the same thing?

For one thing, they both end with round brackets () i.e sum() & .append(). How are they different? I wrote another article to explain just that. Read it here

This article is getting too long already. Let's wrap it up. The entire code we will need for this simple program can be found below:

# a dictionary
student = {
    "maths": 10,
    "english": 30,
    "programming": 50,
    "accounting": 40
}

# an empty list that will contain the student's mark
student_marks = []

# the iteration
for score  in student.values():

    #the collection
    student_marks.append(score)

# get the sum of scores
total_score = sum(student_marks)

# get the average score
average = total_score / len(student_marks)

# check if the average score is below/ above 50 to give remark
if average < 50:
    remark = "FAIL"
else:
    remark = "PASS"

# print out the information for the student parent to see ๐Ÿ˜
print("Student's result:")
print(f"Total: {total_score}; Average: {average} %; Remark: {remark}")

You might need to delete all the codes we've written previously. Then copy, paste and run this one to avoid stress ๐Ÿ˜‰.

The final Output:

>>> Student's result:
>>> Total: 130; Average: 32.5%; Remark: FAIL

Okay! That's it for now. You just got yourself one step ready to take on professional projects in the real world.

Good job! The world awaits you!

Now you must have noticed that the approach we took in writing this program is very limited and it will be very strenuous to use this program for an institution with thousands of students.

Another thing to note is that we hard-coded the student information directly in our code. This usually is not a good practice. But what is hard-coding? you may ask and why is it not a good practice? The next article will discuss this and provide a better approach to solving this problem.

Click here to read it

If you enjoyed going through this article, please leave a comment, a reaction, or share it with your friends on Twitter most especially.

Please find the subscription form either above or below this write-up

Thanks ๐Ÿ˜‰

Photo credit: Deposit Photos

ย