Python Program to Calculate Grade of Student
In this article, you will learn and get code in python to calculate grade of students. Here are the list of programs on finding grade of students:
- Calculate Grade of Student based on Marks obtained in 5 Subjects
- based on Marks obtained in n number of Subjects
The grade must be calculated as per following rules:
Average Mark | Grade |
---|---|
91-100 | A1 |
81-90 | A2 |
71-80 | B1 |
61-70 | B2 |
51-60 | C1 |
41-50 | C2 |
33-40 | D |
21-32 | E1 |
0-20 | E2 |
Calculate Grade of Student in Python
This program find and prints grade of student based on marks obtained in 5 subjects entered by user at run-time.
To calculate grade of students in python, you have to ask from user to enter marks obtained in 5 subjects. Now calculate the sum of all the marks and then calculate the average marks to find the grade according to the average marks obtained by student as shown in the program given below:
Following python program asks from user to enter marks obtained in 5 subjects to calculate and print the grade of student:
print("Enter Marks Obtained in 5 Subjects: ") markOne = int(input()) markTwo = int(input()) markThree = int(input()) markFour = int(input()) markFive = int(input()) tot = markOne+markTwo+markThree+markFour+markFive avg = tot/5 if avg>=91 and avg<=100: print("Your Grade is A1") elif avg>=81 and avg<91: print("Your Grade is A2") elif avg>=71 and avg<81: print("Your Grade is B1") elif avg>=61 and avg<71: print("Your Grade is B2") elif avg>=51 and avg<61: print("Your Grade is C1") elif avg>=41 and avg<51: print("Your Grade is C2") elif avg>=33 and avg<41: print("Your Grade is D") elif avg>=21 and avg<33: print("Your Grade is E1") elif avg>=0 and avg<21: print("Your Grade is E2") else: print("Invalid Input!")
Here are some sample runs of the above python program to demonstrate how to calculate the grade of a student.
When you run the above code, you will see the following sample output asking from the user to enter marks obtained in 5 subjects:
Now enter marks obtained in 5 subjects say 89, 99, 98, 92, 77 and then press ENTER
key to see the grade obtained according to marks entered as shown in the snapshot given below:
Note - If mark contains decimal values like 98.6, 86.9 etc., Then replace int with float.
That is, to handle with floating-point (value containing decimal) values, replace the following block of code:
markOne = int(input()) markTwo = int(input()) markThree = int(input()) markFour = int(input()) markFive = int(input())
with the block of code given below:
markOne = float(input()) markTwo = float(input()) markThree = float(input()) markFour = float(input()) markFive = float(input())
Receive Marks using Loop and Find Grade
This program uses for loop to receive marks obtained in 5 subjects. It also uses for loop to find the total mark:
mark = [] tot = 0 print("Enter Marks Obtained in 5 Subjects: ") for i in range(5): mark.insert(i, input()) for i in range(5): tot = tot + int(mark[i]) avg = tot/5 if avg>=91 and avg<=100: print("Your Grade is A1") elif avg>=81 and avg<91: print("Your Grade is A2") elif avg>=71 and avg<81: print("Your Grade is B1") elif avg>=61 and avg<71: print("Your Grade is B2") elif avg>=51 and avg<61: print("Your Grade is C1") elif avg>=41 and avg<51: print("Your Grade is C2") elif avg>=33 and avg<41: print("Your Grade is D") elif avg>=21 and avg<33: print("Your Grade is E1") elif avg>=0 and avg<21: print("Your Grade is E2") else: print("Invalid Input!")
Here is its sample run with user input, 89, 76, 78, 45, 35 as marks obtained in 5 subjects:
Note - You can also use append() in place of insert(). To do, just replace the following statement:
mark.insert(i, input())
with the statement given below:
mark.append(input())
The following block of code (from above program):
for i in range(5): mark.insert(i, input())
is created to execute the following statement:
mark.insert(i, input())
five times from 0 to 4. That is, this statements gets executed five times with the value of i from 0 to 4.
When i's value is 0 (at first execution), then mark entered by user gets stored in the list at mark[0]. Now at second time, the value of i is 1, so mark entered by user gets stored in the list at mark[1], and so on upto 5 times.
Receive Marks of N Subjects and Find Grade
This program allows user to enter the number of subjects along with marks obtained in all subjects. That is, this program find and prints grade of student based on marks obtained in N subjects. The value of N and marks in N subjects must be entered by user:
mark = [] tot = 0 print("Enter Number of Subjects: ") subNo = int(input()) print("Enter Marks Obtained in " + str(subNo) + " Subjects: ") for i in range(subNo): mark.append(input()) for i in range(subNo): tot = tot + int(mark[i]) avg = tot/subNo if avg>=91 and avg<=100: print("Grade = A1") elif avg>=81 and avg<91: print("Grade = A2") elif avg>=71 and avg<81: print("Grade = B1") elif avg>=61 and avg<71: print("Grade = B2") elif avg>=51 and avg<61: print("Grade = C1") elif avg>=41 and avg<51: print("Grade = C2") elif avg>=33 and avg<41: print("Grade = D") elif avg>=21 and avg<33: print("Grade = E1") elif avg>=0 and avg<21: print("Grade = E2") else: print("Invalid Input!")
Here is its sample run with user input, 6 as number of subjects and 56, 46, 76, 87, 45, 37 as marks obtained in 6 subjects:
Here is another sample run with user input, 10 as total number of subjects, and 56, 76, 87, 90, 45, 44, 34, 56, 76, 80 as marks obtained in 10 subjects:
Post a Comment