Back to Blog
Developer guide
PYTHONJune 21, 2026

Python ValueError Examples and Fixes for Developers

Python ValueError errors can be frustrating and time-consuming to debug, especially when working on critical projects. In this article, we'll explore the most common Python ValueError examples, their causes, and provide actionable solutions to help you resolve them efficiently. Whether you're a beginner or an experienced developer, understanding these errors will save you time and improve your overall coding skills.

1. Unordered Elements in a List

A ValueError occurs when trying to sort an unordered list using the sorted() function or the list.sort() method.

Why It Happens

This error happens because the list contains unsortable elements, such as objects or custom classes without a __lt__() method.

How to Fix It

To fix this error, ensure that the list contains only elements that can be sorted using the built-in comparison operators. If the list contains custom objects, implement the __lt__() method to define the sorting order.


2. Invalid Date Format

A ValueError arises when trying to parse a date string with an incorrect format using the datetime.strptime() function.

Why It Happens

This error occurs because the date string does not match the specified format, such as a string with leading zeros missing in a YYYY-MM-DD format.

How to Fix It

To resolve this error, double-check the date string and ensure it matches the specified format. You can also use the datetime.datetime.strptime() function's strict and fuzzy parsing modes to handle date strings with varying formats.


3. Non-numeric Data in a Float Conversion

A ValueError occurs when trying to convert a string containing non-numeric data to a float using the float() function or the str.replace() method.

Why It Happens

This error happens because the string contains characters that cannot be converted to a float, such as leading or trailing whitespace, or non-numeric characters.

How to Fix It

To fix this error, use the str.strip() method to remove leading and trailing whitespace before attempting the float conversion. You can also use regular expressions to remove non-numeric characters from the string.


4. Out-of-range Values in a Range Function

A ValueError arises when trying to use the range() function with an out-of-range start or stop value.

Why It Happens

This error occurs because the start or stop value is less than the start value or greater than the stop value, which is invalid for the range function.

How to Fix It

To resolve this error, ensure that the start value is less than or equal to the stop value. You can also use the math.ceil() or math.floor() functions to round the start or stop value to the nearest integer, depending on your use case.


5. Invalid Input in a Function Call

A ValueError occurs when passing invalid input to a function, such as a function expecting a string but receiving an integer.

Why It Happens

This error happens because the function expects a specific type or format of input, but the provided input does not match those expectations.

How to Fix It

To fix this error, check the function's documentation and ensure that the input matches the expected type and format. You can also use type hints and validation to catch and handle invalid input before it reaches the function call.


6. Missing Required Argument in a Function Call

A ValueError arises when omitting a required argument in a function call.

Why It Happens

This error occurs because the function expects a specific argument to be present, but it is missing.

How to Fix It

To resolve this error, ensure that all required arguments are present and correctly formatted when calling the function. You can also use the ** operator to pass keyword arguments and avoid missing required arguments.


7. Invalid Format Specifier in a Format String

A ValueError occurs when using an invalid format specifier in a format string, such as %d or %s with the wrong data type.

Why It Happens

This error happens because the format specifier does not match the data type or format of the provided data.

How to Fix It

To fix this error, double-check the format specifier and ensure it matches the data type and format of the provided data. You can also use the str.format() method or f-strings to create formatted strings with more flexibility and less risk of ValueError errors.

Conclusion

Python ValueError errors can be challenging to debug, but understanding their causes and solutions can save you time and improve your coding skills. By following the steps outlined in this article, you'll be better equipped to handle common ValueError errors and write more robust, error-free code. Remember to always check the documentation, validate your input, and handle exceptions to ensure your code runs smoothly and efficiently.

Explore More Debugging Resources

- [Browse all PYTHON errors](/languages/python)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allPython errors

Related PYTHON Articles

Have a specific error? Get an instant explanation.

Explain an Error