Student Grade Calculator
Learn how to create a student grade calculator using Python variables, arithmetic operations, conditions, functions and percentage calculations.
1. Project Introduction
A Student Grade Calculator takes marks of different subjects and calculates the total marks, percentage and final grade.
This project is useful for understanding how real-world data can be processed using Python.
2. Taking Student Name
First, we can ask the student to enter their name.
name = input("Enter student name: ")
3. Taking Subject Marks
We can take marks for each subject separately.
english = float(input("English marks: "))
maths = float(input("Maths marks: "))
science = float(input("Science marks: "))
computer = float(input("Computer marks: "))
gk = float(input("General Knowledge marks: "))
We use float() so that decimal values can also be accepted.
4. Calculating Total Marks
Add all subject marks to calculate the total.
total = (
english +
maths +
science +
computer +
gk
)
If there are five subjects and each subject has a maximum of 100 marks, the maximum total is 500.
5. Calculating Percentage
Percentage can be calculated using:
percentage = (total / 500) * 100
For example, if a student gets 400 out of 500:
percentage = (400 / 500) * 100 # 80.0
6. Understanding Grade Conditions
Now we can use if / elif / else to assign a grade.
if percentage >= 90:
grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 50:
grade = "D"
else:
grade = "F"
7. Checking Pass or Fail
We can decide whether the student has passed based on the percentage.
if percentage >= 33:
result = "Pass"
else:
result = "Fail"
8. Creating a Grade Function
Instead of keeping all grade conditions in the main program, we can create a function.
def calculate_grade(percentage):
if percentage >= 90:
return "A+"
elif percentage >= 80:
return "A"
elif percentage >= 70:
return "B"
elif percentage >= 60:
return "C"
elif percentage >= 50:
return "D"
return "F"
9. Using a Function
grade = calculate_grade(percentage)
print("Grade:", grade)
Functions make the program easier to understand, reuse and maintain.
10. Validating Marks
Marks should normally be between 0 and 100.
if marks < 0 or marks > 100:
print("Invalid marks")
This prevents incorrect values from being processed.
11. Displaying the Result
print("Student:", name)
print("Total:", total)
print("Percentage:", percentage)
print("Grade:", grade)
print("Result:", result)
12. Complete Project Flow
13. Concepts You Practiced
- Python Variables
- input()
- float()
- Arithmetic Operators
- if / elif / else
- Functions
- return
- Validation
- Percentage Calculation
- Logical Conditions
14. Run Complete Project 🚀
You have learned the complete logic of the Student Grade Calculator. Now open the working project.
Run Complete Project