Contact

Python Program to Find Grade of a Student

This comprehensive guide provides a series of Python programs for calculating and analyzing student grades and marks. Each program addresses a unique aspect, offering code, sample outputs, and brief description.

Find Grade of a Student using If-Else

This program prompts the user to input marks in five subjects and calculates the average to determine the student's grade.

Python Code
print("Enter Marks Obtained in 5 Subjects: ")
markOne = float(input())
markTwo = float(input())
markThree = float(input())
markFour = float(input())
markFive = float(input())

total_marks = markOne + markTwo + markThree + markFour + markFive
average = total_marks / 5

if 91 <= average <= 100:
    print("Your Grade is A1")
elif 81 <= average < 91:
    print("Your Grade is A2")
elif 71 <= average < 81:
    print("Your Grade is B1")
elif 61 <= average < 71:
    print("Your Grade is B2")
elif 51 <= average < 61:
    print("Your Grade is C1")
elif 41 <= average < 51:
    print("Your Grade is C2")
elif 33 <= average < 41:
    print("Your Grade is D")
elif 21 <= average < 33:
    print("Your Grade is E1")
elif 0 <= average < 21:
    print("Your Grade is E2")
else:
    print("Invalid Input!")
Output
Enter Marks Obtained in 5 Subjects:
85
92
78
67
95
Your Grade is A1

The program begins by prompting the user to input the marks obtained in five subjects. The float(input()) statements are used to convert the user's input (which is initially a string) into floating-point numbers.

The program then uses a series of if, elif (else-if), and else statements to determine the student's grade based on the calculated average. Each if or elif block checks if the average falls within a specific range and prints the corresponding grade message. The final else statement handles the case where the average is not within any expected range, printing "Invalid Input!"

Find Grade of a Student using For Loop

This program utilizes a loop to receive marks obtained in five subjects and calculates the average grade.

Python Code
marks = []
total_marks = 0
print("Enter Marks Obtained in 5 Subjects: ")
for i in range(5):
    marks.append(float(input()))

for mark in marks:
    total_marks += mark
average = total_marks / 5

if 91 <= average <= 100:
    print("Your Grade is A1")
elif 81 <= average < 91:
    print("Your Grade is A2")
elif 71 <= average < 81:
    print("Your Grade is B1")
elif 61 <= average < 71:
    print("Your Grade is B2")
elif 51 <= average < 61:
    print("Your Grade is C1")
elif 41 <= average < 51:
    print("Your Grade is C2")
elif 33 <= average < 41:
    print("Your Grade is D")
elif 21 <= average < 33:
    print("Your Grade is E1")
elif 0 <= average < 21:
    print("Your Grade is E2")
else:
    print("Invalid Input!")
Output
Enter Marks Obtained in 5 Subjects:
72
89
56
73
60
Your Grade is B1

Find Grade of a Student using Dictionary

This program uses a dictionary to store grade ranges and their corresponding labels, providing a structured and readable approach.

Python Code
def calculate_grade(average):
    grade_scale = {
        (91, 100): "A1",
        (81, 91): "A2",
        (71, 81): "B1",
        (61, 71): "B2",
        (51, 61): "C1",
        (41, 51): "C2",
        (33, 41): "D",
        (21, 33): "E1",
        (0, 21): "E2",
    }

    for score_range, grade in grade_scale.items():
        if score_range[0] <= average <= score_range[1]:
            return grade

    return "Invalid Input!"

def main():
    marks = []
    total_marks = 0

    print("Enter Marks Obtained in 5 Subjects:")
    for _ in range(5):
        marks.append(float(input()))

    for mark in marks:
        total_marks += mark

    average = total_marks / 5

    grade = calculate_grade(average)

    print(f"Your Grade is {grade}")

if __name__ == "__main__":
    main()
Output
Enter Marks Obtained in 5 Subjects:
78
92
85
70
60
Your Grade is B1

Student Marksheet Program in Python

This program allows the user to input the number of subjects and corresponding marks to generate a comprehensive student marksheet.

Python Code
def calculate_grade(average):
    grade_scale = {
        (91, 100): "A1",
        (81, 91): "A2",
        (71, 81): "B1",
        (61, 71): "B2",
        (51, 61): "C1",
        (41, 51): "C2",
        (33, 41): "D",
        (21, 33): "E1",
        (0, 21): "E2",
    }

    for score_range, grade in grade_scale.items():
        if score_range[0] <= average <= score_range[1]:
            return grade

    return "Invalid Input!"

def generate_marksheet(student_name, marks):
    total_marks = sum(marks)
    average = total_marks / len(marks)
    grade = calculate_grade(average)

    print("\n***** Student Marksheet *****")
    print(f"Student Name: {student_name}")
    print("Subject-wise Marks:")
    for i, mark in enumerate(marks, start=1):
        print(f"Subject {i}: {mark}")
    print(f"Total Marks: {total_marks}")
    print(f"Average: {average:.2f}")
    print(f"Grade: {grade}")
    print("*****************************")

def main():
    print("Student Marksheet Program in Python")
    student_name = input("Enter Student Name: ")

    num_subjects = int(input("Enter the number of subjects: "))
    marks = []

    print("\nEnter Marks Obtained in each Subject:")
    for _ in range(num_subjects):
        marks.append(float(input()))

    generate_marksheet(student_name, marks)

if __name__ == "__main__":
    main()
Output
Student Marksheet Program in Python
Enter Student Name: Jane Smith
Enter the number of subjects: 4

Enter Marks Obtained in each Subject:
78
92
85
70

***** Student Marksheet *****
Student Name: Jane Smith
Subject-wise Marks:
Subject 1: 78.0
Subject 2: 92.0
Subject 3: 85.0
Subject 4: 70.0
Total Marks: 325.0
Average: 81.25
Grade: A2
*****************************

Python Grade Calculator with Loops

This program allows users to input marks for multiple subjects using a loop and calculates the average grade.

Python Code
def calculate_grade(average):
    grade_scale = {
        (91, 100): "A1",
        (81, 91): "A2",
        (71, 81): "B1",
        (61, 71): "B2",
        (51, 61): "C1",
        (41, 51): "C2",
        (33, 41): "D",
        (21, 33): "E1",
        (0, 21): "E2",
    }

    for score_range, grade in grade_scale.items():
        if score_range[0] <= average <= score_range[1]:
            return grade

    return "Invalid Input!"

def generate_marksheet(student_name, marks):
    total_marks = sum(marks)
    average = total_marks / len(marks)
    grade = calculate_grade(average)

    print("\n***** Student Marksheet *****")
    print(f"Student Name: {student_name}")
    print("Subject-wise Marks:")
    for i, mark in enumerate(marks, start=1):
        print(f"Subject {i}: {mark}")
    print(f"Total Marks: {total_marks}")
    print(f"Average: {average:.2f}")
    print(f"Grade: {grade}")
    print("*****************************")

def main():
    print("Python Grade Calculator with Loops")
    student_name = input("Enter Student Name: ")

    marks = []
    while True:
        try:
            mark = float(input("Enter Marks (or type 'stop' to finish): "))
            marks.append(mark)
        except ValueError:
            stop_input = input("Do you want to stop? (yes/no): ").lower()
            if stop_input == 'yes':
                break
            else:
                print("Invalid input. Please enter a valid numeric value or 'stop'.")

    generate_marksheet(student_name, marks)

if __name__ == "__main__":
    main()
Output
Python Grade Calculator with Loops
Enter Student Name: Alex Johnson

Enter Marks (or type 'stop' to finish): 78
Enter Marks (or type 'stop' to finish): 92
Enter Marks (or type 'stop' to finish): 85
Enter Marks (or type 'stop' to finish): 70
Enter Marks (or type 'stop' to finish): stop

***** Student Marksheet *****
Student Name: Alex Johnson
Subject-wise Marks:
Subject 1: 78.0
Subject 2: 92.0
Subject 3: 85.0
Subject 4: 70.0
Total Marks: 325.0
Average: 81.25
Grade: A2
*****************************



Sharing is stylish! Gift your friends this awesome read!