Back to Blog
Developer guide
PYTHONMay 27, 2026

Python ValueError Examples and Fixes for Developers

When working with Python, developers often encounter ValueError exceptions that can halt the execution of their code. ValueError is a broad exception that can occur due to a variety of reasons, making it challenging to identify and fix the issue. In this article, we'll explore some common Python ValueError examples and provide step-by-step fixes to help you overcome these errors and improve your code's reliability.

1. Invalid Literal for int() with Base 10

This error occurs when the input string cannot be converted to an integer using base 10. For example, trying to convert a string with non-numeric characters to an integer will result in a ValueError.

Why It Happens

The cause of this error is typically a string that contains non-numeric characters or a string that is not a valid integer representation.

How to Fix It

To fix this error, ensure that the input string only contains numeric characters. You can use the str.isdigit() method to check if the string consists only of digits. If the string is not a valid integer representation, consider using a different data type or conversion method.


2. Not Enough Values to Unpack

This error occurs when the number of values in the tuple or list is less than the number of variables in the unpacking operation.

Why It Happens

The cause of this error is typically a mismatch between the number of values and the number of variables in the unpacking operation.

How to Fix It

To fix this error, ensure that the number of values in the tuple or list matches the number of variables in the unpacking operation. You can use the * operator to unpack the remaining values into a list or tuple.


3. Invalid Date Format

This error occurs when the date string does not match the expected format. For example, trying to parse a date string with a different format than the one specified will result in a ValueError.

Why It Happens

The cause of this error is typically a date string that does not match the expected format.

How to Fix It

To fix this error, ensure that the date string matches the expected format. You can use the datetime.strptime() function to parse the date string and specify the format using the % directive.


4. No JSON Object Could Be Decoded

This error occurs when the JSON string is malformed or cannot be decoded. For example, trying to parse a JSON string with a missing or invalid value will result in a ValueError.

Why It Happens

The cause of this error is typically a malformed or invalid JSON string.

How to Fix It

To fix this error, ensure that the JSON string is well-formed and valid. You can use the json.loads() function to parse the JSON string and catch any exceptions that occur during decoding.


5. Invalid Literal for float() with Base 10

This error occurs when the input string cannot be converted to a float using base 10. For example, trying to convert a string with decimal points to a float will result in a ValueError if the string is not a valid float representation.

Why It Happens

The cause of this error is typically a string that contains non-numeric characters or a string that is not a valid float representation.

How to Fix It

To fix this error, ensure that the input string only contains numeric characters. You can use the str.replace() method to remove any non-numeric characters before attempting to convert the string to a float.

Conclusion

Python ValueError exceptions can be challenging to debug due to their broad nature. However, by understanding the common causes of these errors and following the fixes outlined in this article, you can improve your code's reliability and avoid the frustration of dealing with unexpected errors. Remember to always validate user input, check for invalid data formats, and use try-except blocks to catch and handle ValueError exceptions.

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