Automation and Artificial Intelligence
A script processes interface counters until it encounters zero, skipping invalid values. What does it print? ```python total = 0 attempts = 0 for raw in ["2", "bad", "0", "4"]: try: value = int(raw) if value == 0: break except ValueError: continue else: total += value finally: attempts += 1 print(total, attempts) ```
Show the answer
Correct answerD. 2 3
AWhy not this: The finally block also runs when the handled ValueError leads to continue, so attempts reaches 3.
BWhy not this: The loop stops at zero, so there is no fourth execution of finally.
CWhy not this: The zero triggers break, so the final value is never processed or added.
DWhy this is right: Only 2 is added, and finally runs for all three attempted values, including those that trigger continue and break.
Only 2 reaches the else block. The finally block runs for the successful conversion, the handled exception, and the break, so attempts reaches 3. Counting every input overlooks that break prevents processing "4".