Student Grade Calculator Program in Python
Convert a student's marks into a letter grade in Python using an if-elif chain, then extend it to total, percentage and pass or fail for five subjects.
- Time
O(1) for one mark; O(s) for s subjects - Space
O(1); O(s) for the list of subject marks - Hints3
- Read4 min
A student grade calculator in Python reads marks with float(input()), rejects values below 0 or above 100, and then checks grade bands from the highest down: if marks >= 90 the grade is A+, elif marks >= 80 it is A, and so on, with else giving F. Checking from the top down means each elif only needs a lower limit. For 92, 67 and 35 the grades are A+, C and F on the scale used here.
Problem statement
Write a student grade calculator program in Python. Read a student's marks out of 100 and print the grade using this scale:
| Marks | Grade |
|---|---|
| 90 to 100 | A+ |
| 80 to 89 | A |
| 70 to 79 | B |
| 60 to 69 | C |
| 50 to 59 | D |
| 40 to 49 | E |
| Below 40 | F |
Print "Invalid marks" for anything below 0 or above 100. Show the output for three different inputs.
This is a sample scale for practice. Schools and universities use their own grade bands, so change the numbers to match yours.
A number marks.
"Grade: <grade>" or "Invalid marks".
Examples
marks = 92Grade: A+92 >= 90, so the first condition is True.
marks = 67Grade: C67 fails >= 90, >= 80 and >= 70, then passes >= 60.
Constraints
- 0 <= marks <= 100 for a valid result
- Marks may be decimals, such as 39.5
Think before you code
Reveal the questions to ask yourself first
- If you check marks >= 90 first, what do you already know when you reach the elif marks >= 80 line?
- What should happen to 105 or -5?
- Is 39.5 a pass or a fail on this scale?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Marks to grade with if-elif
Python checks an if-elif chain from top to bottom and runs only the first branch
whose condition is True. So:
- First, reject marks below 0 or above 100.
- Then check
marks >= 90. If True, the grade is A+, and nothing else is checked. - If not, check
marks >= 80. You already know the marks are below 90, so this branch means 80 to 89. - Continue down to
>= 40. Anything left is F.
Why the order matters
| Order | Marks = 95 | Result |
|---|---|---|
>= 90 first, then >= 80 |
>= 90 is True |
A+ (correct) |
>= 40 first, then >= 50 ... |
>= 40 is True |
E (wrong) |
If the lowest band comes first, almost every mark matches it. Always go from the top
band down, or write full ranges like 80 <= marks < 90. For more on the chain, see
if-else statements in Python.
Dry run
Walk through the example step by step
Three inputs through the chain:
marks | >=90 | >=80 | >=70 | >=60 | >=50 | >=40 | grade
------+------+------+------+------+------+------+------
92 | yes | | | | | | A+
67 | no | no | no | yes | | | C
35 | no | no | no | no | no | no | F (else)
For 92, the chain stops at the first test. For 35, every test fails and else runs.
Solution
Reveal the full Python solution
def get_grade(marks):
if marks >= 90:
return "A+"
elif marks >= 80:
return "A"
elif marks >= 70:
return "B"
elif marks >= 60:
return "C"
elif marks >= 50:
return "D"
elif marks >= 40:
return "E"
else:
return "F"
for marks in [92, 67, 35]:
print("Marks:", marks, "Grade:", get_grade(marks))Output for three inputs:
Marks: 92 Grade: A+
Marks: 67 Grade: C
Marks: 35 Grade: F
get_grade() returns as soon as one condition is True, so the later checks never run
for that mark.
Python grade program with user input and range check
marks = float(input("Enter marks (0-100): "))
if marks < 0 or marks > 100:
print("Invalid marks")
elif marks >= 90:
print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
elif marks >= 50:
print("Grade: D")
elif marks >= 40:
print("Grade: E")
else:
print("Grade: F (fail)")
Enter marks (0-100): 85
Grade: A
Enter marks (0-100): 40
Grade: E
Enter marks (0-100): 105
Invalid marks
Enter marks (0-100): 39.5
Grade: F (fail)
- 40 is exactly on a boundary.
>=includes it, so it gets E, not F. - 39.5 is below 40, so it is F. Some schools round marks first; if yours does, call
round(marks)before the checks. - 105 is caught by the first check, so it never reaches the grade bands.
Grade calculator for 5 subjects in Python
Most report cards have several subjects. Read each mark, find the total and percentage, give a grade from the percentage, and mark a fail if any subject is below 40.
def get_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"
elif percentage >= 40:
return "E"
else:
return "F"
subjects = ["Maths", "Physics", "Chemistry", "English", "Computers"]
marks = []
for name in subjects:
marks.append(int(input(f"{name} marks: ")))
total = sum(marks)
percentage = total / len(subjects)
print("Total:", total, "/", len(subjects) * 100)
print(f"Percentage: {percentage:.2f}")
print("Grade:", get_grade(percentage))
if min(marks) < 40:
print("Result: Fail (below 40 in at least one subject)")
else:
print("Result: Pass")
Maths marks: 78
Physics marks: 85
Chemistry marks: 69
English marks: 91
Computers marks: 88
Total: 411 / 500
Percentage: 82.20
Grade: A
Result: Pass
Maths marks: 95
Physics marks: 92
Chemistry marks: 35
English marks: 90
Computers marks: 94
Total: 406 / 500
Percentage: 81.20
Grade: A
Result: Fail (below 40 in at least one subject)
The second student has a high percentage but fails Chemistry. min(marks) finds the
lowest subject mark in one call. {percentage:.2f} prints two decimal places.
To check your own numbers without code, use the marks percentage calculator or the weighted grade calculator. For another if-elif program, see simple calculator program in Python.
O(1) for one mark; O(s) for s subjectsO(1); O(s) for the list of subject marksReview before the interview
Common mistakes
- Checking the lowest band first (marks >= 40), so 95 gets E.
- Using separate if statements instead of elif, so one mark prints several grades.
- Writing marks > 90 instead of marks >= 90, so exactly 90 drops to A.
- Forgetting float() or int() around input(). Comparing the string "85" with 90 raises TypeError.
- Skipping the range check, so 150 is graded A+.
Edge cases to test
- Exactly on a boundary (90, 80, ... 40): >= puts it in the higher grade.
- Marks above 100 or below 0: print Invalid marks.
- Decimal marks like 89.5: graded A on this scale unless you round first.
- Non-numeric input like "abc": float() raises ValueError. Wrap it in try-except to ask again.
Interview follow-ups
- How would you store the grade bands in a list and loop over them instead of writing elif?
- How would you grade a whole class from a list of names and marks and count each grade?
- How would you convert the grade to grade points for a CGPA calculation?
Keep practising
Simple Calculator Program in Python
Hints · Dry run · SolutionSolve Similar problemLeap Year Program in Python with Output
Hints · Dry run · SolutionSolve Similar problemFind the Largest of Three Numbers
Hints · Dry run · SolutionSolve Similar problemFind the Average of the Elements in an Array
Hints · Dry run · SolutionSolve Related learningIf Else Statements
TutorialLearn Related learningFunctions For Beginners
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
