{"record":{"id":"b63431987649216a","repo":"TheAlgorithms/Python","slug":"empty-string-was-passed-to-the-function","errorCode":null,"errorMessage":"Empty string was passed to the function","messagePattern":"Empty string was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/binary_to_decimal.py","lineNumber":28,"sourceCode":"    -29\n    >>> bin_to_decimal(\"0\")\n    0\n    >>> bin_to_decimal(\"a\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-binary value was passed to the function\n    >>> bin_to_decimal(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    >>> bin_to_decimal(\"39\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-binary value was passed to the function\n    \"\"\"\n    bin_string = str(bin_string).strip()\n    if not bin_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = bin_string[0] == \"-\"\n    if is_negative:\n        bin_string = bin_string[1:]\n    if not all(char in \"01\" for char in bin_string):\n        raise ValueError(\"Non-binary value was passed to the function\")\n    decimal_number = 0\n    for char in bin_string:\n        decimal_number = 2 * decimal_number + int(char)\n    return -decimal_number if is_negative else decimal_number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":10,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/binary_to_decimal.py#L10-L44","documentation":"bin_to_decimal raises this ValueError when the input, after str() conversion and .strip(), is empty. The guard runs before any parsing, so an empty or all-whitespace string never reaches the digit loop. It exists to distinguish 'no input at all' from 'input that is not binary' (a separate error).","triggerScenarios":"bin_to_decimal(''), bin_to_decimal('   '), bin_to_decimal(None) (str(None)='None' actually fails the binary check instead), bin_to_decimal('  \\t ') — any input that is empty after trimming whitespace.","commonSituations":"Reading user input that was submitted blank; iterating over file lines where a trailing empty line is not filtered; variable initialized to '' and never set before the call.","solutions":["Check the string is non-empty before calling: if not s.strip(): ...","Filter blank lines when processing files: (line for line in lines if line.strip())","Default missing input to '0' if an empty string should mean zero in your domain"],"exampleFix":"# before\nbin_to_decimal(user_input)  # user pressed Enter\n# ValueError: Empty string was passed to the function\n\n# after\nif not user_input.strip():\n    raise ValueError('binary string required')\nbin_to_decimal(user_input)","handlingStrategy":"validation","validationCode":"if not str(bin_string).strip():\n    raise ValueError('binary string required')\nbin_to_decimal(bin_string)","typeGuard":"def is_nonempty_str(v) -> bool:\n    return isinstance(v, str) and v.strip() != ''","tryCatchPattern":"try:\n    bin_to_decimal(s)\nexcept ValueError as e:\n    if 'Empty string' in str(e):\n        return 0  # or skip record\n    raise","preventionTips":["Filter blank lines/tokens before batch conversion","Make binary fields required in input validation","Default empty form fields to '0' explicitly"],"tags":["conversions","binary","validation","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}