Python Program to Convert Score into Grade Using If-Else
In many educational systems, numerical scores are converted into letter grades such as A, B, C, D, or F. Programming languages like Python can easily automate this task using conditional statements.
In this tutorial, we will explain a Python program that asks the user to enter a score between 0.0 and 1.0 and then converts that score into a corresponding letter grade.
This program demonstrates important Python concepts such as user input, conditional statements, logical operators, and error handling.
Python Program
Below is the Python code used to perform the grade calculation.
score = input('Enter score: ')
s = float(score)
if s >= 0.9 and s <= 1.0:
print('A')
elif s >= 0.8 and s < 0.9:
print('B')
elif s >= 0.7 and s < 0.8:
print('C')
elif s >= 0.6 and s < 0.7:
print('D')
elif s < 0.6 and s > 0.0:
print('F')
else:
try:
if s <= 0.0 or s >= 1.0:
print('Error, Score is out of range. Please enter score between 0.0 and 1.0')
except:
quit()
Step-by-Step Explanation
Let us understand how the program works step by step.
1. Taking Input from the User
The program first asks the user to enter a score.
score = input('Enter score: ')
The input() function allows the program to receive data from the user.
Example:
Enter score: 0.85
The value entered by the user is stored in the variable score.
However, the input() function stores values as strings (text).
Example:
score = "0.85"
Since mathematical comparisons require numeric values, the program converts this string into a number.
2. Converting the Score to Float
s = float(score)
The float() function converts the string into a floating-point number (decimal value).
Example:
float("0.85") → 0.85
Now the variable s can be used in numerical comparisons.
3. Checking the Grade Using If-Else Conditions
The program uses if-elif-else statements to determine the correct grade.
Grade A
if s >= 0.9 and s <= 1.0:
print('A')
If the score is between 0.9 and 1.0, the program prints grade A.
Example:
Enter score: 0.95
Output: A
Grade B
elif s >= 0.8 and s < 0.9:
print('B')
If the score is between 0.8 and 0.9, the program prints grade B.
Example:
Enter score: 0.85
Output: B
Grade C
elif s >= 0.7 and s < 0.8:
print('C')
If the score is between 0.7 and 0.8, the program prints grade C.
Example:
Enter score: 0.75
Output: C
Grade D
elif s >= 0.6 and s < 0.7:
print('D')
If the score is between 0.6 and 0.7, the program prints grade D.
Example:
Enter score: 0.65
Output: D
Grade F
elif s < 0.6 and s > 0.0:
print('F')
If the score is below 0.6 but greater than 0, the program prints grade F.
Example:
Enter score: 0.50
Output: F
4. Handling Invalid Input
The program also checks whether the entered score is outside the valid range.
else:
try:
if s <= 0.0 or s >= 1.0:
print('Error, Score is out of range. Please enter score between 0.0 and 1.0')
If the score is:
-
less than 0.0, or
-
greater than 1.0
then the program prints an error message.
Example:
Enter score: 1.5
Error, Score is out of range. Please enter score between 0.0 and 1.0
The try block is used for error handling, although in this specific code it is not strictly necessary.
Example Program Execution
Example 1:
Enter score: 0.92
A
Example 2:
Enter score: 0.84
B
Example 3:
Enter score: 0.73
C
Example 4:
Enter score: 0.40
F
Example 5 (invalid score):
Enter score: 1.2
Error, Score is out of range. Please enter score between 0.0 and 1.0
Why Conditional Statements Are Important
Conditional statements such as if, elif, and else allow programs to make decisions based on certain conditions.
They are used in many real-world applications, including:
-
Student grading systems
-
Banking systems
-
Login authentication
-
Online forms
-
Data validation
Understanding conditional logic is a fundamental skill in programming.
Conclusion
This Python program demonstrates how to convert a numerical score into a letter grade using if-elif-else conditions. It also ensures that the input score falls within the valid range of 0.0 to 1.0.
By practicing such programs, beginners can develop a strong understanding of decision-making logic in Python. Conditional statements form the foundation for building more complex applications such as grading systems, evaluation tools, and automated reporting systems.
0 Comments