Table of contents
Introduction
Grading systems are used to evaluate the academic performance of students. In this tutorial, we will create a simple grading system using Python. This program will ask the user to input the number of subjects they want to be graded on, the subject names, and the scores they obtained in their CATs and final exam. The program will then calculate the total score and assign a grade based on a predefined grading system. Finally, the program will print the grade for each subject along with the name of the user.
Creating the Program
To create the grading system program in Python, we need to follow the following steps:
Step 1: Print the Program Introduction To inform the user about the purpose of the program, we will print a simple introduction to the program. This can be done using the print statement in Python.
print('Hello, this is a grading system by SamY in co-operation with Mr Lasisi and some PFA students')
print("It uses the 100% scoring system")
Step 2: Collect User Details To begin grading the user, we will first ask the user to input their name and the number of subjects they want to be graded on. This can be achieved using the input function in Python.
name = input("Enter your name: ")
numsub = int(input("Enter number of subjects: "))
Step 3: Define Functions To simplify the program, we will define two functions. The first function details()
will ask the user to input the details of each subject, including the subject name. The second function val()
will ask the user to input the scores they obtained in the two CATs and the final exam.
def details():
print("---------------------------------------")
subname = input("Enter name of subject: ")
def val():
ca1 = int(input("Enter your First CAT: "))
...
Step 4: Create a Loop to Iterate through the Subjects To enable the program to loop through the subjects, we will use a for loop in Python. The loop will iterate through the number of subjects the user inputs and call the details()
and val()
functions for each subject.
for x in range(numsub):
details()
val()
x = x - 1
Step 5: Calculate the Total Score and Assign Grades After collecting the scores for each subject, we can now calculate the total score and assign a grade to the user based on the predefined grading system. We can achieve this by adding the scores obtained in each subject and comparing them to the grading system.
subscore = ca1+ca2+exam
while subscore > 100:
print("This score is invalid")
if subscore == 100:
print(name + ", Congratulations! This is an outstanding performance. " + "Your total is", subscore)
elif subscore >= 80:
print(name + ", You got an A. " + "Your total is", subscore)
elif subscore >= 70:
print(name + ", You got a B. " + "Your total is", subscore)
elif subscore >= 60:
print(name + ", You got a C. " + "Your total is", subscore)
elif subscore >= 50:
print(name + ", You got a D. " + "Your total is", subscore)
elif subscore >= 40:
print(name + ", You got a P. " + "Your total is", subscore)
else:
print(name + ", You have failed. " + "Your total is", subscore)
- Get User Input for Number of Subjects After setting up the details function, the program should ask for the number of subjects the user wants to input. This is done using the
input
function, which is used to take user input. The user's input is then converted to an integer using theint
function and stored in the variablenumsub
. This value will be used as a counter for the number of times the loop will run.
numsub = int(input("Enter number of subjects: "))
- Loop through the Subjects The next step is to create a loop that will run for the number of subjects the user entered. This loop will ask for the name of the subject and call the
val
function to get the scores for each subject. To do this, we use afor
loop and therange
function. Therange
function creates a sequence of numbers starting from 0 up tonumsub - 1
. Thefor
loop then runs through this sequence and executes the code within the loop for each number in the sequence.
for x in range(numsub):
details()
val()
- Save Data to an Array Instead of just printing out the results for each subject, we want to save the data to an array. To do this, we need to define an empty array before the loop starts. We can then append the subject name and score to the array at the end of each iteration of the loop.
# Define an empty array to hold the data
data = []
for x in range(numsub):
details()
score = val()
# Append subject name and score to the data array
data.append([subname, score])
- Display the Data Once the loop is done, we can display the data in the array by iterating over the array using another loop. We can use the
enumerate
function to get both the index and the value of each element in the array. This allows us to print out the subject name and score together with the index of the subject in the array.
# Display the data in the array
for i, subject in enumerate(data):
print("Subject", i + 1, ":", subject[0], ", Score:", subject[1])
- Final Touches Finally, we can add some closing messages to the program. We can print a line to separate the output from the rest of the program, and we can print a closing message with the names of the creators of the program.
# Add some closing messages
print("-------------------------------------------")
print("Thanks for trying it out. ")
print("Done by Samuel Yahaya, Mr Lasisi, and some students in PFA Geek Club")
Run the Program With all the code in place, we can now run the program and see how it works. When we run the program, we will be prompted to enter our name and the number of subjects we want to input. We will then be prompted for the name of each subject and the scores for each subject. The program will then display the subject names and scores, along with a closing message.
Conclusion
Congratulations! You have now created a grading system in Python that can take input from the user, save the data to an array, and display the data to the user. With this basic framework, you can modify the code to suit your needs and create more complex grading systems or data entry systems.
Just in case you missed some parts of the code or got lost at some point, You can go to my Github account at SammY949 or click here, where the code is hosted and download it. Don't forget to give it a star.
Thank you for reading this article and thank you for following along to build this project.