Module 5 Assignment 3.3 Answer
3.3 Grade Calculation Based on Score
Question
Write a Python program that:
- Prompts the user to enter a score between 0.0 and 1.0.
- If the score is out of range, print
"Error: Invalid score"
and exit. - If the score is valid, print the corresponding grade based on the table below:
Score Range | Grade |
---|---|
>= 0.9 |
A |
>= 0.8 |
B |
>= 0.7 |
C |
>= 0.6 |
D |
< 0.6 |
F |
Test Case: Enter 0.85
, the expected output should be B
.
Correct Python Code
# Prompt user for input
score = input("Enter Score: ")
try:
# Convert input to float
score = float(score)
# Check if the score is within valid range
if score < 0.0 or score > 1.0:
print("Error: Invalid score")
else:
# Determine the grade based on score
if score >= 0.9:
print("A")
elif score >= 0.8:
print("B")
elif score >= 0.7:
print("C")
elif score >= 0.6:
print("D")
else:
print("F")
except ValueError:
# Handle non-numeric input
print("Error: Invalid input")
Expected Output
Enter Score: 0.85
B
Explanation
- Prompt the user to enter a score.
- Convert input to float (handle errors using
try/except
). - Check if the score is in range (
0.0 to 1.0
), else print"Error: Invalid score"
. - Determine the grade using
if-elif
conditions. - Handle non-numeric inputs using
try/except
to avoid program crashes.
Why This Code is Correct
✅ Ensures valid input (between 0.0
and 1.0
).
✅ Handles non-numeric inputs gracefully.
✅ Uses proper conditional logic for grading.
✅ Produces expected output when tested with 0.85
→ B
.