Predict the output
What does this print?
a, b = [9, 9], [1]
carry, out = 0, []
for i in range(max(len(a), len(b))):
total = carry
total += a[i] if i < len(a) else 0
total += b[i] if i < len(b) else 0
out.append(total % 10)
carry = total // 10
print(out, carry)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [0, 0] 1
Right: 99 + 1 leaves a carry that still needs its own digit.
That leftover carry is why the chain loop also runs while the carry is non-zero.
Why not the others?
- B [0, 0, 1] 0. The loop stops before writing the final carry.
- C [0, 1] 0. The carry passes through both digits.