TheAlgorithms/Python · error · TypeError

num_cuts must be a positive numeric value.

Error message

num_cuts must be a positive numeric value.

What it means

Raised by Circle.max_parts (geometry/geometry.py:166) when num_cuts is not an int/float or is negative. The method computes the maximum number of pieces a circle can be divided into with num_cuts straight cuts, which is only defined for non-negative numeric cut counts.

Source

Thrown at geometry/geometry.py:166

        >>> circle.max_parts(0)
        1.0
        >>> circle.max_parts(7)
        29.0
        >>> circle.max_parts(54)
        1486.0
        >>> circle.max_parts(22.5)
        265.375
        >>> circle.max_parts(-222)
        Traceback (most recent call last):
            ...
        TypeError: num_cuts must be a positive numeric value.
        >>> circle.max_parts("-222")
        Traceback (most recent call last):
            ...
        TypeError: num_cuts must be a positive numeric value.
        """
        if not isinstance(num_cuts, (int, float)) or num_cuts < 0:
            raise TypeError("num_cuts must be a positive numeric value.")
        return (num_cuts + 2 + num_cuts**2) * 0.5


@dataclass
class Polygon:
    """
    An abstract class which represents Polygon on a 2D surface.

    >>> Polygon()
    Polygon(sides=[])
    >>> polygon = Polygon()
    >>> polygon.add_side(Side(5)).get_side(0)
    Side(length=5, angle=Angle(degrees=90), next_side=None)
    >>> polygon.get_side(1)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    >>> polygon.set_side(0, Side(10)).get_side(0)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert string input before calling: circle.max_parts(int(user_input))
  2. Clamp or reject negative values in your own layer: n = max(0, n)
  3. Add isinstance/negative checks at your data boundary so bad values never reach the library

Example fix

# before
count = circle.max_parts(raw_cuts)  # raw_cuts may be "-5" or -5

# after
raw_cuts = int(raw_cuts)
if raw_cuts < 0:
    raise ValueError("cuts must be >= 0")
count = circle.max_parts(raw_cuts)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(num_cuts, (int, float)) or isinstance(num_cuts, bool):
    raise TypeError("num_cuts must be numeric")
if num_cuts < 0:
    raise ValueError("num_cuts must be >= 0")
result = circle.max_parts(num_cuts)

Try / catch

try:
    parts = circle.max_parts(n)
except TypeError:
    parts = None  # invalid input; log and skip

Prevention

When it happens

Trigger: circle.max_parts(-222), circle.max_parts("-222"), or any string/None/list argument. Note the boundary: max_parts(0) is allowed (returns 1); only values < 0 or non-numeric types raise, despite the message saying 'positive'.

Common situations: Feeding user input or CLI args (always strings) directly without int()/float() conversion; looping over values that can go negative due to an off-by-one; passing Decimal or numpy types not covered by isinstance(x, (int, float)) in some configurations.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d1436c1eb5c6e0d1. Report an issue: GitHub.