pythonforbio.
[WASM idle]
Python Basics05/12

Conditionals

Starting sandbox…
Beginnerpuzzle4 tests

if runs a block when a condition is true. elif and else handle the remaining cases.

depth = 45

if depth >= 100:
    print("high")
elif depth >= 30:
    print("adequate")
else:
    print("low")

Only the first matching branch runs. Order matters: if depth >= 30 came first, a depth of 120 would report "adequate".

Comparison and logic

depth = 45
quality = 38

print(depth > 30 and quality > 20)
print(depth > 100 or quality > 20)
print(not depth > 100)
print(30 <= depth <= 100)

Truthiness

Empty values are false. Non-empty values are true.

for value in ["", "ATG", [], [1], 0, 1, None]:
    print(repr(value), bool(value))

This is why if not sequence: is the normal way to check for an empty string.

Conditional expressions

A one-line form is available when both branches produce a value.

depth = 45
label = "pass" if depth >= 30 else "fail"
print(label)

Task

Write classify(depth, quality). Both arguments are integers. Return:

  • "FAIL" if depth is below 10, or quality is below 20.
  • "HIGH" if depth is 100 or more and quality is 30 or more.
  • "PASS" otherwise.

Check the failing case first.

Test cases · 4

#viainputexpected stdout
1stdin5 40FAIL
2stdin120 35HIGH
3stdin45 25PASS
4stdin150 22PASS

Hints · 2

01Hint

Return early from the FAIL branch so later branches only see valid input.

02Hint

Combine the two FAIL conditions with `or`.

starter

No output yet — run the code to populate this drawer.

DictionariesLoops