Why does a Boolean `found` flag report "not found" exactly once, where a naive `else` would print it on every single pass?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
A hidden number is given in secret. Walk through every number from 1 to 10 inclusive with a for loop and use a Boolean flag that starts as False to record whether the secret shows up. Assign the flag's final value to found.
Hint: range(1, 11) yields 1 through 10, and == tests equality.
The secret-number search over a range
Write for i in range(1, 11) with if i == secret: print("found"), and record the output for secret = 4 and for secret = 100.
Reporting a miss once with a found flag
Write found = False before the loop, found = True at the match, and the if not found report after the loop, alongside the failed attempts that place else or break inside it.
Cube-root search with abs and break
Write for guess in range(abs(cube) + 1), the break once guess**3 reaches the target, the if/else that reads off which exit occurred, and the sign flip for negative input.