Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Grade Calculator

Turn marks into letter grades with an ordered Python if-elif chain. Students examine cutoff scores, decimal marks, and why range validation is a separate step.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 3 of 10
grade-calculator.py
1# Take marks as input
2marks = float(input("Enter marks (0-100): "))
3
4# Assign grade based on marks
5if marks >= 90:
6 print("Grade: A")
7elif marks >= 80:
8 print("Grade: B")
9elif marks >= 70:
10 print("Grade: C")
11elif marks >= 60:
12 print("Grade: D")
13else:
14 print("Grade: F")

Output

Enter marks (0-100): 85
Grade: B

Enter marks (0-100): 45
Grade: F

In this one

  • Build descending grade bands
  • Include exact cutoffs
  • Separate prompts from validation

How it works

  1. 1

    Read marks as a float.

  2. 2

    Try cutoffs ninety, eighty, seventy, then sixty.

  3. 3

    Print the first grade matched or F.

Edge cases

What grade does 90 receive?

What grade does 90 receive?

A, because the first comparison includes equality.

Does 105 get rejected?

Does 105 get rejected?

No. It prints A; a real marks form should validate the range first.

What's going on

Grade bands are checked from the highest cutoff downward. marks is a float, so a score such as 89.5 can be compared without discarding its fractional part.

A score of 85 fails the ninety test but passes the eighty test, giving B. Later tests do not run. A score below sixty reaches F. The prompt says zero to one hundred, but this example does not enforce that range.

What grade does 90 receive? A, because the first comparison includes equality.

Does 105 get rejected? No. It prints A; a real marks form should validate the range first.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why start with the highest cutoff?

Pick an answer to check it.

Question 2

What grade does 90 receive?

Pick an answer to check it.

Questions people ask

What grade does 90 receive?

A, because the first comparison includes equality.

Does 105 get rejected?

No. It prints A; a real marks form should validate the range first.

Why start with the highest cutoff?

A high mark also exceeds lower cutoffs.