Assign grades from marks

Assign grades from marks

EasyIf-Else Programs · 3 of 64

Write a Python program to assign a grade from total marks: below 40 Fail, 40-49 C, 50-59 B, 60-69 A, 70-79 A+, 80-89 A++, 90-100 Excellent, anything else Invalid.

Input
35, 47, 68, 85, 100, 105
Output
35 -> Fail
47 -> C
68 -> A
85 -> A++
100 -> Excellent
105 -> Invalid marks

Approach

  1. Reject values outside 0-100 first.
  2. Then test the ranges from the highest down, so each elif only needs a lower bound.

Solution

Python
def grade(marks):    if marks < 0 or marks > 100:        return "Invalid marks"    elif marks >= 90:        return "Excellent"    elif marks >= 80:        return "A++"    elif marks >= 70:        return "A+"    elif marks >= 60:        return "A"    elif marks >= 50:        return "B"    elif marks >= 40:        return "C"    else:        return "Fail" for m in [35, 47, 68, 85, 100, 105]:    print(m, "->", grade(m))
Output
35 -> Fail
47 -> C
68 -> A
85 -> A++
100 -> Excellent
105 -> Invalid marks

Line by line

Lines 2-3

Invalid input is handled first so the other branches can assume 0 to 100.

Lines 4-15

Because Python stops at the first true branch, testing from the top down means marks >= 80 only runs for marks below 90.

Lines 16-17

Anything below 40 falls through to else.

Note

The original task listed “marks > 40: Fail”, which would fail almost everyone. It should be marks below 40.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Numbers from 1 to 30 divisible by 3

Numbers from 1 to 30 divisible by 3

EasyIf-Else Programs · 2 of 64

Write a Python program to print all the numbers from 1 to 30 that are divisible by 3.

Output
3 6 9 12 15 18 21 24 27 30 

Approach

  1. Loop from 1 to 30 and print a number only when n % 3 == 0.

Solution 1: loop with if

Python
for n in range(1, 31):    if n % 3 == 0:        print(n, end=" ")print()
Output
3 6 9 12 15 18 21 24 27 30 

Line by line

Line 1

range(1, 31) stops before 31, so 30 is included.

Line 3

end=" " keeps the numbers on one line.

Line 4

An empty print() ends the line.

Solution 2: range with a step

Python
print(list(range(3, 31, 3)))
Output
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

Line by line

Line 1

Starting at 3 with a step of 3 produces only multiples of 3, so no if is needed.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Check if a number is divisible by 3

Check if a number is divisible by 3

EasyIf-Else Programs · 1 of 64

Write a Python program to check whether a given number is divisible by 3.

Input
21, 22
Output
21 is divisible by 3
22 is not divisible by 3

Approach

  1. A number is divisible by 3 when the remainder n % 3 is 0.

Solution

Python
for n in [21, 22]:    if n % 3 == 0:        print(n, "is divisible by 3")    else:        print(n, "is not divisible by 3")
Output
21 is divisible by 3
22 is not divisible by 3

Line by line

Line 1

The loop tests two sample numbers so both branches run.

Line 2

% gives the remainder; a remainder of 0 means an exact division.

Lines 4-5

else handles every other case.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Python If-Else Programs with Solutions

Python Practice › If-Else Programs

Python If-Else Programs with Solutions

64 programs59 easy5 medium0 hard

64 if-else programs that practise decisions in Python: comparisons, elif chains, nested conditions and combining tests with and / or. Each solution tests several inputs so you can see every branch run.

Before you start

Read the If…Else and Match Case lesson first if the topic is new to you.

All programs