TheAlgorithms/Python · error · TypeError
Input values must either be float or int: {list(locals().val
Error message
Input values must either be float or int: {list(locals().values())} What it means
Raised by convert_to_2d (graphics/vector3_for_2d_rendering.py:32) when any of its five arguments (x, y, z, distance, scale) is not a float or int. The function performs 3D-to-2D perspective projection arithmetic, so all five values must be numeric before the projection formulas run.
Source
Thrown at graphics/vector3_for_2d_rendering.py:32
x: float, y: float, z: float, scale: float, distance: float
) -> tuple[float, float]:
"""
Converts 3d point to a 2d drawable point
>>> convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0)
(7.6923076923076925, 15.384615384615385)
>>> convert_to_2d(1, 2, 3, 10, 10)
(7.6923076923076925, 15.384615384615385)
>>> convert_to_2d("1", 2, 3, 10, 10) # '1' is str
Traceback (most recent call last):
...
TypeError: Input values must either be float or int: ['1', 2, 3, 10, 10]
"""
if not all(isinstance(val, (float, int)) for val in locals().values()):
msg = f"Input values must either be float or int: {list(locals().values())}"
raise TypeError(msg)
projected_x = ((x * distance) / (z + distance)) * scale
projected_y = ((y * distance) / (z + distance)) * scale
return projected_x, projected_y
def rotate(
x: float, y: float, z: float, axis: str, angle: float
) -> tuple[float, float, float]:
"""
rotate a point around a certain axis with a certain angle
angle can be any integer between 1, 360 and axis can be any one of
'x', 'y', 'z'
>>> rotate(1.0, 2.0, 3.0, 'y', 90.0)
(3.130524675073759, 2.0, 0.4470070007889556)
>>> rotate(1, 2, 3, "z", 180)
(0.999736015495891, -2.0001319704760485, 3)View on GitHub (pinned to f5988cc097)
Solutions
- Convert each argument at the call site: convert_to_2d(float(x), float(y), float(z), float(distance), float(scale))
- Sanitize incoming data once at the boundary (parser/config loader), not at every call
- If using Decimal/Fraction, convert to float first — the function only accepts the built-in numeric types
Example fix
# before
point_2d = convert_to_2d(row["x"], row["y"], row["z"], distance, scale) # row values are str
# after
point_2d = convert_to_2d(
float(row["x"]), float(row["y"]), float(row["z"]),
float(distance), float(scale),
) Defensive patterns
Strategy: type-guard
Validate before calling
x, y, z, distance, scale = (float(v) for v in (x, y, z, distance, scale)) projected = convert_to_2d(x, y, z, distance, scale)
Type guard
def all_numeric(*vals: object) -> bool:
return all(isinstance(v, (int, float)) and not isinstance(v, bool) for v in vals) Try / catch
try:
p = convert_to_2d(x, y, z, d, s)
except TypeError as exc:
raise ValueError(f"bad 3D point data: {exc}") from exc Prevention
- Coerce to float where data enters your program, not per call
- Reject bool explicitly if 1/0-as-bool would silently distort coordinates
When it happens
Trigger: convert_to_2d("1", 2, 3, 10, 10) — one string poisons all inputs; also None, lists, or bools (bool passes the check since bool subclasses int, which can silently produce 1/0 coordinates).
Common situations: Reading coordinates from JSON/CSV where numbers arrive as strings; passing values straight from a GUI form or argparse without float(); passing Decimal or Fraction objects that are numeric but not int/float instances.
Related errors
- Axis must be a str
- Input values except axis must either be float or int: {list(
- Input value must be a positive integer
- Input value must be a 'int' type
- Input value must be a 'int' type
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d488d2899b1a2077.
Report an issue: GitHub.