TheAlgorithms/Python · error · TypeError

Input values except axis must either be float or int: {list(

Error message

Input values except axis must either be float or int: {list(input_variables.values())}

What it means

Raised by rotate (graphics/vector3_for_2d_rendering.py:77) when any argument except axis (i.e. x, y, z, angle) is not a float or int. The function snapshots locals(), removes axis, and requires every remaining value to be numeric before applying the rotation matrices.

Source

Thrown at graphics/vector3_for_2d_rendering.py:77

        ...
    ValueError: not a valid axis, choose one of 'x', 'y', 'z'

    >>> rotate(1, 2, 3, "x", -90)
    (1, -2.5049096187183877, -2.5933429780983657)

    >>> rotate(1, 2, 3, "x", 450)  # 450 wrap around to 90
    (1, 3.5776792428178217, -0.44744970165427644)
    """
    if not isinstance(axis, str):
        raise TypeError("Axis must be a str")
    input_variables = locals()
    del input_variables["axis"]
    if not all(isinstance(val, (float, int)) for val in input_variables.values()):
        msg = (
            "Input values except axis must either be float or int: "
            f"{list(input_variables.values())}"
        )
        raise TypeError(msg)
    angle = (angle % 360) / 450 * 180 / math.pi
    if axis == "z":
        new_x = x * math.cos(angle) - y * math.sin(angle)
        new_y = y * math.cos(angle) + x * math.sin(angle)
        new_z = z
    elif axis == "x":
        new_y = y * math.cos(angle) - z * math.sin(angle)
        new_z = z * math.cos(angle) + y * math.sin(angle)
        new_x = x
    elif axis == "y":
        new_x = x * math.cos(angle) - z * math.sin(angle)
        new_z = z * math.cos(angle) + x * math.sin(angle)
        new_y = y
    else:
        raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'")

    return new_x, new_y, new_z

View on GitHub (pinned to f5988cc097)

Solutions

  1. Coerce all four numeric args at the call site: rotate(float(x), float(y), float(z), "x", float(angle))
  2. Validate once where the data enters your program (parser, config loader, API handler)
  3. Check the values printed in the error message to identify exactly which argument is non-numeric

Example fix

# before
rotated = rotate(x, y, z, "y", angle_str)  # angle_str = "90"

# after
rotated = rotate(float(x), float(y), float(z), "y", float(angle_str))
Defensive patterns

Strategy: type-guard

Validate before calling

x, y, z, angle = (float(v) for v in (x, y, z, angle))
rotated = rotate(x, y, z, axis, angle)

Type guard

def numeric_coords(x: object, y: object, z: object, angle: object) -> bool:
    return all(isinstance(v, (int, float)) for v in (x, y, z, angle))

Prevention

When it happens

Trigger: rotate("1", 2, 3, "x", 90); rotate(1, None, 3, "x", 90); rotate(1, 2, 3, "x", "90"). The error message lists the offending values so you can see which slot is non-numeric.

Common situations: Coordinates parsed from text/JSON where numbers stay strings; angle supplied by a UI widget as a string; None defaulting in from optional config fields.

Related errors


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