Grade Calculator
Calculate letter grade (A, B, C, D, F) from percentage using conditional statements.
BeginnerTopic: Conditional Programs
What You'll Learn
- Using if-elif-else chains for ranges
- Validating numeric input
- Mapping continuous values to categories
Python Grade Calculator Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to calculate grade from percentage
percentage = float(input("Enter your percentage: "))
if percentage < 0 or percentage > 100:
print("Invalid percentage. Please enter a value between 0 and 100.")
else:
if percentage >= 90:
grade = 'A'
elif percentage >= 80:
grade = 'B'
elif percentage >= 70:
grade = 'C'
elif percentage >= 60:
grade = 'D'
else:
grade = 'F'
print(f"Your grade is: {grade}")Output
Enter your percentage: 85 Your grade is: B
Step-by-Step Breakdown
- 1Read percentage from the user.
- 2Check that it lies between 0 and 100.
- 3Use if-elif-else to assign a letter grade.
- 4Print the resulting grade.
Understanding Grade Calculator
We map a numeric percentage to a letter grade using an if-elif-else ladder:
•90–100 → A
•80–89 → B
•70–79 → C
•60–69 → D
•Below 60 → F
We also validate that the percentage is between 0 and 100 before grading.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.