TheAlgorithms/Python · error · ValueError
not a valid axis, choose one of 'x', 'y', 'z'
Error message
not a valid axis, choose one of 'x', 'y', 'z'
What it means
Raised by rotate (graphics/vector3_for_2d_rendering.py:92) when axis is a string but not one of 'x', 'y', 'z'. The function dispatches on the axis value with if/elif branches; the final else branch rejects anything else.
Source
Thrown at graphics/vector3_for_2d_rendering.py:92
"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
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) = }")
print(f"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }")
View on GitHub (pinned to f5988cc097)
Solutions
- Normalize the axis before calling: axis = axis.strip().lower() and verify membership in {'x','y','z'}
- Fix the literal to a lowercase 'x', 'y', or 'z'
- Constrain UI/config axis fields to a choice list so invalid values cannot reach the library
Example fix
# before
rotated = rotate(1, 2, 3, "X", 90) # uppercase -> ValueError
# after
axis = "X".strip().lower()
if axis not in {"x", "y", "z"}:
raise ValueError(f"unknown axis {axis!r}")
rotated = rotate(1, 2, 3, axis, 90) Defensive patterns
Strategy: validation
Validate before calling
axis = axis.strip().lower() if isinstance(axis, str) else axis
if axis not in {"x", "y", "z"}:
raise ValueError(f"axis must be 'x', 'y' or 'z', got {axis!r}")
rotated = rotate(x, y, z, axis, angle) Try / catch
try:
rotated = rotate(x, y, z, axis, angle)
except ValueError:
axis = "z" # or surface an error to the user Prevention
- Normalize case/whitespace on axis input at the boundary
- Restrict axis fields in forms/config to a fixed choice set
When it happens
Trigger: rotate(1, 2, 3, "n", 90); rotate(1, 2, 3, "X", 90) — uppercase fails because matching is case-sensitive; rotate(1, 2, 3, "xy", 90).
Common situations: User-typed axis names with different case or whitespace ('X', ' x '); localization or alternative naming ('horizontal', 'z-axis'); config typos.
Related errors
- Axis must be a str
- Input values except axis must either be float or int: {list(
- Expected a_coeffs to have {self.order + 1} elements for {sel
- Expected b_coeffs to have {self.order + 1} elements for {sel
- k must not be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/99bdc96dd790886d.
Report an issue: GitHub.