Input percentage marks and display the grade using the CBSE grading scale
Expt. No.07
Page No.14
Grade from percentage
Aim
To write a Python program that accepts a student's percentage and displays the grade (A1 to E).
Concepts used
Algorithm
- Input the percentage.
- If it is below 0 or above 100, it is invalid.
- Check the ranges from the highest down: 91 and above A1, 81 A2, 71 B1, 61 B2, 51 C1, 41 C2, 33 D, below 33 E.
- Display the grade.
Program
# Display the grade for a percentage (CBSE grading scale) percent = float(input("Enter your percentage: "))if percent < 0 or percent > 100: grade = "Invalid percentage"elif percent >= 91: grade = "A1"elif percent >= 81: grade = "A2"elif percent >= 71: grade = "B1"elif percent >= 61: grade = "B2"elif percent >= 51: grade = "C1"elif percent >= 41: grade = "C2"elif percent >= 33: grade = "D"else: grade = "E"print("Grade:", grade)
A marked line continues the line above it. In your file, write it on the same line.
Output
Run 1
$ python grade_from_percentage.py
Enter your percentage: 86
Grade: A2
Run 2
$ python grade_from_percentage.py
Enter your percentage: 38
Grade: D
Run 3
$ python grade_from_percentage.py
Enter your percentage: 104
Grade: Invalid percentage
✓ Printed by running this program, not typed by hand.
Result
86% got grade A2, 38% got grade D, and 104 was rejected as an invalid percentage.
Viva voce: what the examiner may ask
01Why are the ranges checked from the highest to the lowest?
Each elif runs only if all conditions above it were false, so percent >= 81 already means the percentage is below 91.
02Why is the validity check placed first?
Otherwise 104 would satisfy percent >= 91 and get A1.
03What does or do in the first condition?
The condition is True if either part is True, so values below 0 or above 100 are both caught.
Variations for the lab test
- Input marks in five subjects, find the percentage and then the grade.
- Print a remark such as "Excellent" for A1 and A2.
- Count how many of ten students got each grade.
! Common mistake Writing the conditions as
percent >= 33 first: every passing student would get D, because the first true branch wins.