Code Challenge
Return None when a search finds nothing, while preserving index zero as a valid match.
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
Return None when a search finds nothing, while preserving index zero as a valid match.
Show the worked solution
def locate(values, wanted):
for index, value in enumerate(values):
if value == wanted:
return index
return None
result = locate(["Python", "SQL"], "Python")
print("Missing" if result is None else result)
Check the result
The result is 0, correctly representing a match at the beginning.
Test one more case
Search for an absent title and confirm that the missing branch now runs.