{"record":{"id":"c087847856e573b9","repo":"TheAlgorithms/Python","slug":"not-a-number","errorCode":null,"errorMessage":"Not a Number","messagePattern":"Not a Number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"maths/geometric_mean.py","lineNumber":33,"sourceCode":"        ...\n    TypeError: Not a Number\n    >>> compute_geometric_mean(5, 125)\n    25.0\n    >>> compute_geometric_mean(1, 0)\n    0.0\n    >>> compute_geometric_mean(1, 5, 25, 5)\n    5.0\n    >>> compute_geometric_mean(2, -2)\n    Traceback (most recent call last):\n        ...\n    ArithmeticError: Cannot Compute Geometric Mean for these numbers.\n    >>> compute_geometric_mean(-5, 25, 1)\n    -5.0\n    \"\"\"\n    product = 1\n    for number in args:\n        if not isinstance(number, int) and not isinstance(number, float):\n            raise TypeError(\"Not a Number\")\n        product *= number\n    # Cannot calculate the even root for negative product.\n    # Frequently they are restricted to being positive.\n    if product < 0 and len(args) % 2 == 0:\n        raise ArithmeticError(\"Cannot Compute Geometric Mean for these numbers.\")\n    mean = abs(product) ** (1 / len(args))\n    # Since python calculates complex roots for negative products with odd roots.\n    if product < 0:\n        mean = -mean\n    # Since it does floating point arithmetic, it gives 64**(1/3) as 3.99999996\n    possible_mean = float(round(mean))\n    # To check if the rounded number is actually the mean.\n    if possible_mean ** len(args) == product:\n        mean = possible_mean\n    return mean\n\n\nif __name__ == \"__main__\":","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/geometric_mean.py#L15-L51","documentation":"Raised by compute_geometric_mean in maths/geometric_mean.py when any element of args is neither int nor float. The function multiplies all arguments together to compute the geometric mean via an n-th root, so non-numeric values would break the arithmetic; each element is checked with isinstance and TypeError('Not a Number') is raised on the first offender.","triggerScenarios":"Calling compute_geometric_mean(2, '3'), compute_geometric_mean([1,2,3]) (passing a list as a single arg), or with None/Decimal/bool-adjacent objects. Note bool passes isinstance(x, int), and a nested list fails.","commonSituations":"Passing an unspread list instead of *args; mixed data from JSON where numbers arrive as strings; None values from sparse data not filtered out.","solutions":["Spread collections: call compute_geometric_mean(*nums) not compute_geometric_mean(nums).","Coerce numeric strings and Decimals to float before calling.","Filter None/missing values out of the data before computing the mean."],"exampleFix":"// before\nmean = compute_geometric_mean(data)  # data is a list -> TypeError\n\n// after\nmean = compute_geometric_mean(*data)","handlingStrategy":"type-guard","validationCode":"clean = [float(x) for x in args if x is not None]\nmean = compute_geometric_mean(*clean)","typeGuard":"def all_numeric(seq) -> bool:\n    return all(isinstance(x, (int, float)) for x in seq)","tryCatchPattern":"try:\n    m = compute_geometric_mean(*nums)\nexcept TypeError as e:\n    if 'Not a Number' in str(e):\n        nums = [float(x) for x in nums]\n        m = compute_geometric_mean(*nums)","preventionTips":["Spread lists with * when calling","Filter None and coerce numeric strings before computing aggregates"],"tags":["math","statistics","geometric-mean","typeerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}