TheAlgorithms/Python · error · ValueError

No solution exists!

Error message

No solution exists!

What it means

Raised by centripetal() in physics/centripetal_force.py when mass is negative. The function returns (mass * v**2) / radius for centripetal force; negative mass is unphysical, so it is rejected. Note only mass < 0 is rejected — a mass of exactly 0 is accepted and returns 0.0, and the velocity sign is squared away (negative velocities are fine, per the doctests).

Source

Thrown at backtracking/rat_in_maze.py:136

    Traceback (most recent call last):
        ...
    ValueError: Invalid source or destination coordinates
    """
    size = len(maze)
    # Check if source and destination coordinates are Invalid.
    if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or (
        not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1)
    ):
        raise ValueError("Invalid source or destination coordinates")
    # We need to create solution object to save path.
    solutions = [[1 for _ in range(size)] for _ in range(size)]
    solved = run_maze(
        maze, source_row, source_column, destination_row, destination_column, solutions
    )
    if solved:
        return solutions
    else:
        raise ValueError("No solution exists!")


def run_maze(
    maze: list[list[int]],
    i: int,
    j: int,
    destination_row: int,
    destination_column: int,
    solutions: list[list[int]],
) -> bool:
    """
    This method is recursive starting from (i, j) and going in one of four directions:
    up, down, left, right.
    If a path is found to destination it returns True otherwise it returns False.
    Parameters
        maze: A two dimensional matrix of zeros and ones.
        i, j : coordinates of matrix
        solutions: A two dimensional matrix of solutions.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check the parameter order: centripetal(mass, velocity, radius).
  2. Validate mass >= 0 before calling if mass comes from external data.
  3. Catch ValueError for user-supplied inputs and surface a clear message.

Example fix

# before
centripetal(-30, 15.5, 10)  # args swapped: velocity in mass slot

# after
centripetal(15.5, -30, 10)  # mass=15.5, velocity=-30, radius=10 -> 1395.0
Defensive patterns

Strategy: validation

Validate before calling

if mass < 0:
    raise ValueError(f"mass must be >= 0, got {mass}")
f = centripetal(mass, velocity, radius)

Type guard

def is_valid_mass(m: object) -> bool:
    return isinstance(m, (int, float)) and not isinstance(m, bool) and m >= 0

Try / catch

try:
    f = centripetal(m, v, r)
except ValueError as e:
    if "mass" in str(e):
        raise ValueError("check argument order: centripetal(mass, velocity, radius)") from e
    raise

Prevention

When it happens

Trigger: centripetal(-10, 15, 5); passing a signed mass from a system that encodes direction/charge in the mass sign; argument-order confusion (passing a negative velocity into the mass slot).

Common situations: Argument order mistakes — signature is centripetal(mass, velocity, radius) and velocity may legitimately be negative (e.g. centripetal(15.5, -30, 10) is valid), so a swapped call like centripetal(-30, 15.5, 10) raises.

Related errors


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