Grade Calculator in Python
90 A, 80 B, ... else F. Check 0–100 first or a 107 becomes an A.
BeginnerConditional ProgramsExample 1 of 20
grade-calculator.py
1# Program to calculate grade from percentage23percentage = float(input("Enter your percentage: "))45if percentage < 0 or percentage > 100:6 print("Invalid percentage. Please enter a value between 0 and 100.")7else:8 if percentage >= 90:9 grade = 'A'10 elif percentage >= 80:11 grade = 'B'12 elif percentage >= 70:13 grade = 'C'14 elif percentage >= 60:15 grade = 'D'16 else:17 grade = 'F'1819 print(f"Your grade is: {grade}")
Output
Enter your percentage: 85 Your grade is: B
In this one
- test the highest grade first
- 107 is invalid, not an A
What's going on
Walk the thresholds from the top. >= 90 then >= 80 and so on. Reverse that order and 95 matches >= 60 first and you hand out a D.
107% should not be an A. The 0–100 check is not decoration.
elif stops at the first hit. That's why 85 is B, not B and C.
After this program
If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.