Build a Simple Exam Grade Calculator in Python
This blog tutorial will teach you how to build a simple examination grade calculator using Python, in an easy to understand manner with full source code and in depth code explanation with detailed explanation so that you will also understand the main concepts of Python that are applied here to build this app. This Python tutorial is for absolute beginners who are learning Python and if you are in an advanced or intermediate phase of doing Python, you may find it boring to learn this project!
CORE CONCEPTS OF THE PROJECT
Here, you are going to learn the steps involved in building an exam grade calculator app using Python and the main concepts of this project is having the following information, that is, what is the name of the examination or subject exam, what is the score of the candidate, what is the full score or total marks expected for the respective subject or examination and then, based on the exam score of the candidate, you need to be able to calculate the percentage score of the exam for that candidate and also the grade scored by him or her for that exam.
PYTHON TOPICS YOU WILL LEARN HERE
You will learn how to create variables in Python, and you will also learn how to use the following built-in functions, namely, print, input function, round function , float function , round function and int function while doing these calculation for this project. You will also learn how to use the if-elif-else conditional loop while building this project.
Let’s understand the print() function
The print() function is an inbuilt python function that is used to implement the print statement or rather this function will print out the statement that you have written as a string within the print() function as a parameter within the parenthesis of the print() function. Without the print() function you will not be able to print out the output of any code on the computer screen. In the code below, you can see that the code starts with the print function where you write the code as
print(“Exam Grade Calculator”) and this will execute the statement Exam Grade Calculator on the screen so that you know that this project that you are going to do is about learning how to build an Exam Grade Calculator because, this statement is going to show up as a title in the first line.
Let’s understand the input() function
The input() function is another inbuild python function that is used to create variables that store values or data based on what is received as input from the users who are using the computer keyboard to key in the data to the computer. In the code below, you can see that the variable that is created by the name of maxScore is using the input function to get the data input from the user. The other variable that is being created is yourScore.
The other variable that is being created is yourScore.
Let’s understand the int() function
The int function is also another in built python function that is used in order to convert any number or decimals to an integer. In the below case we use int function since it is understood that examination marks is supposed to be in integers and not decimals. So even if the user does not give a whole number as an input, the output will show you a number that is a whole number or an integer.
Let’s understand the float() function
The float() function is an in-built function in python and is used to convert an integer or real number into a floating-point number, that is, a decimal number or a fractional number. So, if you use the float function , for example, float(2) will give the output of 2.0 so, what it did is converted the integer 2 to decimal number 2.0.
Let’s understand the round() function
The round() function is another in-built function in python that is used to round off decimal numbers to a specific number of decimal point numbers. For example, if we consider a decimal number such as 2.67895 that has 5 decimal points and if we want to reduce it to two decimal points then I use the round() function, hence round(2.678952) where the second parameter is the number of digits that you want to round it off to, will yield a result of 2.68 as the round function helped this decimal number to round off the decimal number to two positions, as you can see that there are six digits after the decimal point and the round function reduced the six digits to two digits after the decimal.
Let’s understand the if-elif -else loop
The if-elif loop in 21python is used to make conditional statements and yield results based on the different conditions. For example consider a loop such as follows and this is usually used in conditions where there are three or more conditional situatioins such as follows:
print(“x is a negative number”)
print(“x is a positive number”)
In the above example, for the above if-elif-else loop, what it means is that for the first condition if the number x is less than 0, then, we are asked to print out that x is a negative number and in a second condition which is represented by elif, had the number x been equal to 0, then we print out that x has a value of zero and in the third condition, represented by else, we print out that x is a positive number, because, we have already exhausted two conditions, namely, x is less than zero, whereby x is a negative number and if x is equal to zero, than x has a value of zero , so, the only condition left is that x is more than zero, and if it is more than zero, then, obviously, it is a positive number, so, we show this last condition by write else: and then we print out the the last possibility for the value of x as shown above.
The basic code and concepts that are to be used in building this app have already been explained in the above paragraphs, now, we start explaining, step-by-step, the process of building this app.
We use the print() function to print out the title of the app by wrting print(“Exam Grade Calculator”)
CREATING VARIABLES USING THE INT() FUNCTION
Next step, we create a variable and name it examName and this is to name the exam for which we are calculating the grades. Since this is a user based variable, in order to get values of the exam names from the user or candidates, we use the input() function and store the values of the exam names that we get as input that are keyed in by the user into the computer and store these values of exam names in the variable examNames. So, how do we write the code for this ?
Next, we create another variable called maxScore and also get the values for this variable by using the input() function whereby asking the users to input the maximum possible score for this exam by keying down the values that can be stored in this variable for doing our calculation later on. This code syntax is written as :
Next, we need to create another variable and call it , say, yourScore, in order to get the values of scores obtained in the exams by different users or candidates. The code for this is as follows:
Now that we have created the above variables to get started with our exam calculations, let us see what we need to do next!
CREATING VARIABLES TO CALCULATE SCORE
We create a variable called percentScore to calculate the percentage score for the exam and here we need to perform the actual calculation to get the output.
The above calculation is actually performing division and multiplication. You are dividing yourScore by maxScore and then multiplying the resultant number by 100 to get the percentage marks. Here you will note that we are using the float() function while performing the calculation, since we want to get the exact decimal number result after performing the calculation. Some marks may be in the form of fractions, and we want to convert those fractions to decimal numbers while calculating the result to get more accurate exam marks!
The next step would involve rounding off the percentage score to two decimal places and for performing this task, we use the round() function as shown below in the code.
We use the print() function to print out the result as shown here. If you notice carefully, the sentence Your Percentage score is is within strings followed by a comma, and then we write the variable which in this case percentScore and remember, we can not use strings for a variable.
USING IF-ELIF-ELSE LOOP TO WRITE MARKS & GRADE
This loop helps us to evaluate different conditions of the marks obtained in order to calculate the percentage score and also the grade in different situationsl The detailed explanation is given below the following code.
If the person scores more than 90,% or he gets 90%, he gets an A grade, if he gets more than 80 % , or 80%, he also gets A grade , if he gets more than or equal to 70%,he gets a B grade and if he gets a percentage of greater or equal to 60% the he gets a C grade and if gets more than or equal to 50%, he gets a D grade and if he gets less than 50% , he gets a U grade.
As you can see in the loop that if gives the output for the first conditional statement, elif gives the output for various other conditions following the first condition, in case the first condition is not met, and the last condition is mett by the else in the loop as else will bring out the result for the last condition in the loop when all the other conditions are not met in the loop, in which case, the last condition is met by default.
We also show the different emotions like grinning face, angry face etc based on the various output of the conditions. For showing the various emojis, we use the CLDR short names and this is reqpresented by writing the emotion within a curly brace, for example, for showing ‘grinning face’, we write :
For showing an emoji for angry face, we write :
print("Exam Grade Calculator") examName = input("What is the name of the exam?: ") maxScore = int(input("Maximum Possible Score: ")) yourScore = int(input("Your Score: ")) percentScore = float(yourScore/maxScore * 100) percentScore = round(percentScore, 2) print("Your Percentage Score is: ", percentScore) if (percentScore >= 90): print("You got ", percentScore, "%", "which is a A+","\N{smiling face with sunglasses}") elif (percentScore >= 80): print("You got ", percentScore, "% which is a A", "\N{face with tears of joy}") elif (percentScore >= 70): print("You got ", percentScore, "%, which is a B", "\N{grinning face}") elif (percentScore >= 60): print("You got ", percentScore, "%, which is a C", "\N{zipper-mouth face}") elif (percentScore >= 50): print("You got ", percentScore, "%, which is a D","\N{unamused face}") else: print("You got ", percentScore, "%, which is a U", "\N{angry face}")
in few lines of code! Do remember to comment, follow and
share this post among your friends if you find this useful.
Originally published at https://meerateachestech.blogspot.com.