## Creating a Basic Student Grade Calculator in Python

In this blog, we will learn the creation of a basic student grade calculator using Python. This calculator will allow students to input their grades and calculate their overall average easily.

## Method 1: Basic Arithmetic Calculation

The first method involves arithmetic calculations. We will prompt the user to input their grades for each subject and then calculate the average by summing all the grades and dividing by the total number of subjects.

```
def calculate_average(grades):
return sum(grades) / len(grades)
def main():
num_subjects = int(input("Enter the number of subjects: "))
grades = []
for i in range(num_subjects):
grade = float(input(f"Enter grade for subject {i+1}: "))
grades.append(grade)
average = calculate_average(grades)
print("Your average grade is:", average)
if __name__ == "__main__":
main()
```

### Output:

```
Enter the number of subjects: 3
Enter grade for subject 1: 85
Enter grade for subject 2: 90
Enter grade for subject 3: 75
Your average grade is: 83.33333333333333
```

In this method, we define a function `calculate_average`

to compute the average of a list of grades. We then prompt the user to input the number of subjects and their corresponding grades. Finally, we call the `calculate_average`

function to compute the average grade and display the result.

## Method 2: Weighted Average Calculation

In some cases, subjects may carry different weights in the overall grade calculation. Our second method introduces the concept of weighted averages. Each grade is multiplied by its corresponding weight, and the sum of these products is divided by the total weight.

```
def calculate_weighted_average(grades, weights):
total_weight = sum(weights)
weighted_sum = sum(grade * weight for grade, weight in zip(grades, weights))
return weighted_sum / total_weight
def main():
num_subjects = int(input("Enter the number of subjects: "))
grades = []
weights = []
for i in range(num_subjects):
grade = float(input(f"Enter grade for subject {i+1}: "))
weight = float(input(f"Enter weight for subject {i+1}: "))
grades.append(grade)
weights.append(weight)
average = calculate_weighted_average(grades, weights)
print("Your weighted average grade is:", average)
if __name__ == "__main__":
main()
```

### Output:

```
Enter the number of subjects: 3
Enter grade for subject 1: 85
Enter weight for subject 1: 0.3
Enter grade for subject 2: 90
Enter weight for subject 2: 0.4
Enter grade for subject 3: 75
Enter weight for subject 3: 0.3
Your weighted average grade is: 83.0
```

This method enhances the previous one by incorporating weights for each subject. We prompt the user to input both the grade and weight for each subject, and then calculate the weighted average using the provided weights.

## Method 3: Handling Letter Grades

In many educational systems, grades are represented as letters (e.g., A, B, C). Our third method extends the calculator to handle letter grades. We will assign numerical values to letter grades and then calculate the average as before.

```
def letter_to_numeric_grade(letter_grade):
grade_mapping = {'A': 90, 'B': 80, 'C': 70, 'D': 60, 'F': 0}
return grade_mapping.get(letter_grade.upper(), None)
def main():
num_subjects = int(input("Enter the number of subjects: "))
grades = []
for i in range(num_subjects):
grade = input(f"Enter grade for subject {i+1} (A/B/C/D/F): ")
numeric_grade = letter_to_numeric_grade(grade)
if numeric_grade is not None:
grades.append(numeric_grade)
else:
print("Invalid grade entered. Please enter A/B/C/D/F.")
average = calculate_average(grades)
print("Your average grade is:", average)
if __name__ == "__main__":
main()
```

### Output:

```
Enter the number of subjects: 3
Enter grade for subject 1 (A/B/C/D/F): A
Enter grade for subject 2 (A/B/C/D/F): B
Enter grade for subject 3 (A/B/C/D/F): C
Your average grade is: 83.33333333333333
```

This method allows users to input letter grades instead of numerical values. We define a function `letter_to_numeric_grade`

to convert letter grades to their corresponding numeric values, then proceed with the average calculation as before.

For further assistance and learning about Python programming, you can visit Python Tutor.

## Comments (0)