Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON STRUCTURES / CHAPTER 06 / Learn / LESSON 08
=== assert (for tests, not user input) ===
assert condition, "message"
If the condition is False, Python raises AssertionError and stops — “fail loud” in tests. It does not replace if for input() validation.
def find_index(items, target):
for i, value in enumerate(items):
if value == target:
return i
return -1assert find_index([3, 1, 4], 4) == 2 assert find_index([3, 1, 4], 9) == -1 print(find_index(["a", "b"], "b"))
Use assert after the function to check expected behavior. Task: write find_index, asserts, then print one real result.
Learn: assert for tests + search.
AWAITING TYPES
›