← All coding practice

Coding Practice · Beginner · October 14, 2026 · 5 min read

Python Coding Exercise #38: Grades and Points

Part of the series Python Practice Exercise — part 38

Before you start: Know input() and int() · Know if, elif and else · Know comparison operators

The question

The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the table.

PointsGrade
< 0impossible!
0-49fail
50-591
60-692
70-793
80-894
90-1005
> 100impossible!

Some examples:

How many points [0-100]: 37
Grade: fail
How many points [0-100]: 76
Grade: 3
How many points [0-100]: -3
Grade: impossible!

My Solution

points = int(input("How many points [0-100]: "))
if 0 <= points <= 49:
    print("Grade: fail")
elif 50 <= points <= 59:
    print("Grade: 1")
elif 60 <= points <= 69:
    print("Grade: 2")
elif 70 <= points <= 79:
    print("Grade: 3")
elif 80 <= points <= 89:
    print("Grade: 4")
elif 90 <= points <= 100:
    print("Grade: 5")
else:
    print("Grade: impossible!")

How My Solution Works

1. One branch for each row of the table

The table has several ranges of points, and each range gets its own branch. The conditions are written with chained comparisons. 50 <= points <= 59 means “points is at least 50 and at most 59”, just like the range in the table. (Chained comparisons were introduced in Exercise #36.)

Python checks the branches from top to bottom and runs the first one that’s true:

PointsFirst true conditionPrinted
370 <= points <= 49Grade: fail
7670 <= points <= 79Grade: 3
9590 <= points <= 100Grade: 5

2. The else statement

The two “impossible” rows of the table, below 0 and above 100, aren’t written as conditions at all. They are handled by the final else. If a number doesn’t fit any of the six valid ranges, it must be below 0 or above 100, so it gets Grade: impossible!. For -3, none of the six conditions is true, and the else runs.

3. Check the boundaries

The edges of each range are where mistakes usually hide. Here is every boundary, with the grade the program prints:

PointsGradePointsGrade
-1impossible!703
0fail793
49fail804
501894
591905
6021005
692101impossible!

Each grade starts and ends exactly where the table says, and the program prints the right grade for every one of them. (More about testing limits in Exercise #32.)

University Solution

The University Solution takes a different approach:

points = int(input("How many points [0-100]: "))

if points < 0 or points > 100:
    grade = "impossible!"
elif points < 50:
    grade = "fail"
elif points < 60:
    grade = "1"
elif points < 70:
    grade = "2"
elif points < 80:
    grade = "3"
elif points < 90:
    grade = "4"
else:
    grade = "5"

print(f"Grade: {grade}")

There are three differences.

1. The impossible points are checked first. points < 0 or points > 100 catches both impossible cases right away, using or from Exercise #37. After that, every later branch knows that points is between 0 and 100.

2. Each branch only has an upper limit. Because each branch can rely on the earlier ones having failed, there is no need to write the lower limit as well. By the time Python reaches elif points < 60, it already knows the points are at least 50, because the points < 50 branch would have caught anything lower. So points < 60 really means “50 to 59”. The final else means “90 or more”, up to 100. This is the same idea as in Exercise #36, where the order of the branches made the later conditions simpler.

3. The grade is stored in a variable and printed once. Each branch only sets grade, and a single print(f"Grade: {grade}") at the end prints it. The wording Grade: ... is written only once, instead of in seven different print() calls. (The same idea appeared in Exercise #14.) Notice that the grades are stored as strings, such as "1", because they are only ever printed, never used in a calculation.

My Solution and the University Solution print identical results for every whole number from -100 to 300.

A hidden difference: the gaps between ranges

For whole numbers, the two versions always agree. But the ranges in My Solution have tiny gaps between them. The fail range ends at 49, and the next range starts at 50. For whole numbers, nothing sits between 49 and 50. But a decimal number does: 49.5 is not in 0 <= points <= 49, and it’s not in 50 <= points <= 59.

This exercise reads the points with int(), so a decimal can never be entered. But imagine reading them with float() instead, so that a student could have 49.5 points. Here is what each version would print:

PointsMy SolutionUniversity Solution
49.5Grade: impossible!Grade: fail
59.9Grade: impossible!Grade: 1
89.5Grade: impossible!Grade: 4

In My Solution, the decimal falls through all the ranges, lands in the else, and is wrongly called impossible. The University Solution has no gaps, because each branch only says “less than” the next boundary, so every number lands in exactly one range. (The same effect, with <= 20 against < 21, was explained in Exercise #20.)

The lesson: if the ranges are written with both ends, make sure they touch or overlap with no gaps. Writing only the upper limit, and relying on the order of the branches, avoids the problem completely.

Key takeaways

  • Turn each row of a table into one branch of an if / elif / else chain.
  • 50 <= points <= 59 is a chained comparison: “at least 50 and at most 59”.
  • A final else can catch everything that no other branch matched. Here, that’s the impossible points.
  • If you check the impossible cases first, later branches can use only an upper limit, because the earlier branches have already ruled out the lower values.
  • Setting a variable in each branch and printing once at the end avoids repeating the same print().
  • Test every boundary: for each limit, try the value on it and the one next to it.
  • Ranges written with both ends can leave gaps for decimal numbers. Using only upper limits avoids this.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · conditionals · elif · comparison-operators · logical-operators