The Task
Build a program that:
- Asks the user for their name and a test score out of
100. - Checks whether the score is valid, between
0and100. - If valid, assigns a grade based on the score:
- 90 and above → A
- 75 to 89 → B
- 50 to 74 → C
- Below 50 → D
- Prints whether the result is a
Passor aFail. A score of50or above passes. - If the grade is
A, also prints a bonus message:"Excellent work!"
What You’ll Use
input()andint()to take the score- Comparison operators (
>=,<,<=) - Logical operator (
or) if,elif, andelseto assign the grade- A nested
iffor the bonus"Excellent work!"message print()to display the result
Expected Output
Enter your name: Riya
Enter your score: 92
--- Report Card ---
Name: Riya
Score: 92
Grade: A
Result: Pass
Excellent work!Try It Yourself First
Here are a few hints:
- Check for an invalid score, below 0 or above 100, first before checking the grade.
- Use
eliffor the grade ranges, ordered from highest to lowest, just like we saw in the elif lesson. - The
Pass/Failresult depends on the score, not the grade, so it can be its own simpleif/else. - The bonus message only applies when the grade is A, so it belongs inside a nested
if.
Give it a try before you scroll down.
Solution
# Grade Calculator
name = input("Enter your name: ")
score = int(input("Enter your score: "))
print("\n--- Report Card ---")
print("Name:", name)
print("Score:", score)
if score < 0 or score > 100:
print("Invalid score. Please enter a value between 0 and 100.")
else:
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 50:
grade = "C"
else:
grade = "D"
print("Grade:", grade)
if score >= 50:
print("Result: Pass")
else:
print("Result: Fail")
if grade == "A":
print("Excellent work!")How It Works
- The first check,
score < 0orscore > 100, uses theorlogical operator from the operators lesson to catch an invalid score before doing anything else. - The grade is decided using
elif, checked from the highest range to the lowest. Pass/Failis a separate check from the grade because it only depends on whether the score is 50 or above, not on which specific grade was given.- The
"Excellent work!"message uses a nestedifinside theelseblock because it only applies when the score is valid and the grade is A.
Common Mistake
Be careful about the order of your elif conditions.
For example, this is incorrect:
if score >= 50:
grade = "C"
elif score >= 75:
grade = "B"
elif score >= 90:
grade = "A"A score of 95 would incorrectly get a C.
Because Python checks conditions from top to bottom. Since 95 >= 50, is already True, Python assigns C and skips the remaining conditions.
That’s why we check the highest range first:
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 50:
grade = "C"
else:
grade = "D"Challenges
If you want to go further, try these:
- Add an F grade: Change the grading system so scores below 35 receive an F instead of a D.
- Compare Two Scores: Ask the user for two scores and print which one is higher.
- Add Another Bonus Message: Add a message for students who receive a D, encouraging them to try again.
👉 Next tutorial: Python Match Case Statements