Allot a stream from marks

EasyIf-Else Programs · 64 of 64

Write a Python program that allots a stream: marks above 85 Science, above 70 Commerce, 35 to 70 Arts, below 35 Fail.

Advertisement
Input
88, 72, 50, 20
Output
88 -> Science
72 -> Commerce
50 -> Arts
20 -> Fail
70 -> Arts
35 -> Arts

Approach

  1. Test from the highest band down so every mark lands in exactly one stream.

Solution

Python
def stream(marks):    if marks > 85:        return "Science"    elif marks > 70:        return "Commerce"    elif marks >= 35:        return "Arts"    else:        return "Fail" for m in [88, 72, 50, 20, 70, 35]:    print(m, "->", stream(m))
Output
88 -> Science
72 -> Commerce
50 -> Arts
20 -> Fail
70 -> Arts
35 -> Arts

Line by line

Lines 2-9

Using >= for 35 and falling through from the band above means boundary values such as 70 and 35 are covered. The original ranges (35 < marks < 70) left 35 and 70 with no stream.

Advertisement

Learn the concept

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

More if-else programs

← All 64 If-Else Programs