TheAlgorithms/Python · error · TypeError
Not a Number
Error message
Not a Number
What it means
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.
Source
Thrown at maths/geometric_mean.py:33
...
TypeError: Not a Number
>>> compute_geometric_mean(5, 125)
25.0
>>> compute_geometric_mean(1, 0)
0.0
>>> compute_geometric_mean(1, 5, 25, 5)
5.0
>>> compute_geometric_mean(2, -2)
Traceback (most recent call last):
...
ArithmeticError: Cannot Compute Geometric Mean for these numbers.
>>> compute_geometric_mean(-5, 25, 1)
-5.0
"""
product = 1
for number in args:
if not isinstance(number, int) and not isinstance(number, float):
raise TypeError("Not a Number")
product *= number
# Cannot calculate the even root for negative product.
# Frequently they are restricted to being positive.
if product < 0 and len(args) % 2 == 0:
raise ArithmeticError("Cannot Compute Geometric Mean for these numbers.")
mean = abs(product) ** (1 / len(args))
# Since python calculates complex roots for negative products with odd roots.
if product < 0:
mean = -mean
# Since it does floating point arithmetic, it gives 64**(1/3) as 3.99999996
possible_mean = float(round(mean))
# To check if the rounded number is actually the mean.
if possible_mean ** len(args) == product:
mean = possible_mean
return mean
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
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.
Example fix
// before mean = compute_geometric_mean(data) # data is a list -> TypeError // after mean = compute_geometric_mean(*data)
Defensive patterns
Strategy: type-guard
Validate before calling
clean = [float(x) for x in args if x is not None] mean = compute_geometric_mean(*clean)
Type guard
def all_numeric(seq) -> bool:
return all(isinstance(x, (int, float)) for x in seq) Try / catch
try:
m = compute_geometric_mean(*nums)
except TypeError as e:
if 'Not a Number' in str(e):
nums = [float(x) for x in nums]
m = compute_geometric_mean(*nums) Prevention
- Spread lists with * when calling
- Filter None and coerce numeric strings before computing aggregates
When it happens
Trigger: 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.
Common situations: Passing an unspread list instead of *args; mixed data from JSON where numbers arrive as strings; None values from sparse data not filtered out.
Related errors
- Cannot Compute Geometric Mean for these numbers.
- List is empty
- List is empty
- successes must be lower or equal to trials
- the function is defined for non-negative integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c087847856e573b9.
Report an issue: GitHub.