bhylu.com

Advertisements

Code Challenge

Treat None as missing while preserving a supplied zero. Print a default only for the truly missing value.

Understand the idea

Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Treat None as missing while preserving a supplied zero. Print a default only for the truly missing value.

Show the worked solution
Python

for value in [None, 0, 4]:
    result = 10 if value is None else value
    print(result)

Check the result

The outputs are 10, 0, and 4.

Test one more case

Add an empty string and state whether it should be valid under the intended contract.