Back to Lesson

ValueError on Cast

Catch an error when attempting to cast an invalid string to an integer.

Instructions

  • In a
    try
    block, attempt to assign
    val = int("hello")
    .
  • If it succeeds, print the value.
  • Catch the
    ValueError
    and print
    "Invalid number format"
    .
main.py
main.py