{"record":{"id":"2083e5067d38ef9e","repo":"TheAlgorithms/Python","slug":"please-enter-a-valid-number","errorCode":null,"errorMessage":"Please enter a valid number","messagePattern":"Please enter a valid number","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/decimal_to_fraction.py","lineNumber":37,"sourceCode":"    >>> decimal_to_fraction(0)\n    (0, 1)\n    >>> decimal_to_fraction(-2.5)\n    (-5, 2)\n    >>> decimal_to_fraction(0.125)\n    (1, 8)\n    >>> decimal_to_fraction(1000000.25)\n    (4000001, 4)\n    >>> decimal_to_fraction(1.3333)\n    (13333, 10000)\n    >>> decimal_to_fraction(\"1.23e2\")\n    (123, 1)\n    >>> decimal_to_fraction(\"0.500\")\n    (1, 2)\n    \"\"\"\n    try:\n        decimal = float(decimal)\n    except ValueError:\n        raise ValueError(\"Please enter a valid number\")\n    fractional_part = decimal - int(decimal)\n    if fractional_part == 0:\n        return int(decimal), 1\n    else:\n        number_of_frac_digits = len(str(decimal).split(\".\")[1])\n        numerator = int(decimal * (10**number_of_frac_digits))\n        denominator = 10**number_of_frac_digits\n        divisor, dividend = denominator, numerator\n        while True:\n            remainder = dividend % divisor\n            if remainder == 0:\n                break\n            dividend, divisor = divisor, remainder\n        numerator, denominator = numerator // divisor, denominator // divisor\n        return numerator, denominator\n\n\nif __name__ == \"__main__\":","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/decimal_to_fraction.py#L19-L55","documentation":"Raised by decimal_to_fraction() in maths/decimal_to_fraction.py when its argument cannot be converted to a float. The function immediately does float(decimal) inside a try/except ValueError and re-raises ValueError with this clearer message; accepted inputs include numbers and numeric strings like '1.23e2' and '0.500'.","triggerScenarios":"Calling decimal_to_fraction('abc'), decimal_to_fraction(None), decimal_to_fraction(''), or any string float() rejects. Note float('nan')/float('inf') parse fine and will misbehave later instead.","commonSituations":"Unvalidated user or CSV input ('1,5' with a comma, '1/2' as a fraction string, empty cells, currency symbols); None from an optional field; strings with surrounding whitespace actually work, but non-numeric text does not.","solutions":["Validate/clean the string before calling: strip currency symbols and commas, reject empty values.","If the input may be a fraction string like '1/2', parse it yourself with fractions.Fraction instead.","Consider using fractions.Fraction(str_value) directly — it handles decimals and fraction strings and reduces automatically."],"exampleFix":"# before\ndecimal_to_fraction('1/2')  # ValueError: Please enter a valid number\n\n# after\nfrom fractions import Fraction\nFraction('1/2')  # Fraction(1, 2)\n# or: decimal_to_fraction(float(user_str)) after validating user_str","handlingStrategy":"try-catch","validationCode":"try:\n    value = float(str(user_input).strip())\nexcept ValueError:\n    raise ValueError(f'not a parseable decimal: {user_input!r}') from None","typeGuard":"def is_parseable_decimal(v) -> bool:\n    try:\n        float(v)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    num, den = decimal_to_fraction(raw)\nexcept ValueError as e:\n    if 'valid number' in str(e):\n        raw = raw.replace(',', '.').strip()  # locale fix, then retry once\n        num, den = decimal_to_fraction(raw)\n    else:\n        raise","preventionTips":["Reject empty strings and strip currency symbols/commas before conversion.","Use fractions.Fraction for inputs like '1/2' that are fractions, not decimals."],"tags":["maths","conversion","fractions","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}