Skip to main content
Practice

Indicating Invalid Values with ValueError

The ValueError is raised when a function receives a valid data type but an inappropriate or invalid value.


Situations Where ValueError Occurs

  1. When attempting to convert a non-numeric string to a number: Trying to convert a non-numeric string like 'hello' using int() or float() will raise am error

  2. When attempting to convert a string representing a decimal number to an integer: Converting a string such as '3.14' with int() will aslo result in an error.

Example of ValueError Occurrence
try:
number = int("hello")

except ValueError:
print("Attempted to convert a non-numeric value to a number.")


try:
number = int("3.14")

except ValueError:
print("Attempted to convert a string with a decimal to an int() function.")

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.