Handle a failed conversion

Take your time, follow the code and check your understanding.

Move blocks into orderExceptions
  1. print(value)
  2.     value = 0
  3. except ValueError:
  4.     value = int("not-a-number")
  5. try:
Your answer3 min · no timer

Drag blocks, or use the arrow buttons. Keep the displayed indentation. This task uses the taught sequence; other equivalent programs may also be valid.

Show solution and explanation

The except clause handles conversion failure and assigns the fallback before print.

try:
    value = int("not-a-number")
except ValueError:
    value = 0
print(value)