TheAlgorithms/Python · error · ValueError

Input value must be a positive integer

Error message

Input value must be a positive integer

What it means

Raised by check_args() in physics/horizontal_projectile_motion.py when the angle is outside 1-90 degrees inclusive. The projectile formulas in this module model forward launches only, so 0 (horizontal) and 90 (vertical) are excluded along with everything outside the range. This is a domain restriction, not a type problem — the value is numeric but out of range.

Source

Thrown at bit_manipulation/binary_count_trailing_zeros.py:35

    >>> binary_count_trailing_zeros(4294967296)
    32
    >>> binary_count_trailing_zeros(0)
    0
    >>> binary_count_trailing_zeros(-10)
    Traceback (most recent call last):
        ...
    ValueError: Input value must be a positive integer
    >>> binary_count_trailing_zeros(0.8)
    Traceback (most recent call last):
        ...
    TypeError: Input value must be a 'int' type
    >>> binary_count_trailing_zeros("0")
    Traceback (most recent call last):
        ...
    TypeError: '<' not supported between instances of 'str' and 'int'
    """
    if a < 0:
        raise ValueError("Input value must be a positive integer")
    elif isinstance(a, float):
        raise TypeError("Input value must be a 'int' type")
    return 0 if (a == 0) else int(log2(a & -a))


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or reject out-of-range angles before calling: if not 1 <= angle <= 90, skip or rescale.
  2. Convert radians to degrees with math.degrees() before the call.
  3. Handle the 0-degree (purely horizontal) and 90-degree (vertical) cases with dedicated code, since this module excludes them.

Example fix

# before
dist = horizontal_distance(50, math.radians_result)  # e.g. 0.786 -> ValueError

# after
angle_deg = math.degrees(theta_rad)
if not 1 <= angle_deg <= 90:
    raise ValueError(f"angle {angle_deg} outside projectile range 1-90")
dist = horizontal_distance(50, angle_deg)
Defensive patterns

Strategy: validation

Validate before calling

if not 1 <= angle <= 90:
    raise ValueError(f"angle must be in [1, 90] degrees, got {angle}")
dist = horizontal_distance(init_velocity, angle)

Type guard

def is_valid_launch_angle(a: object) -> bool:
    return isinstance(a, (int, float)) and not isinstance(a, bool) and 1 <= a <= 90

Try / catch

try:
    dist = horizontal_distance(v, angle)
except ValueError as e:
    if "Range is 1-90" in str(e):
        angle = min(max(angle, 1), 90)  # clamp only if acceptable for your use
        dist = horizontal_distance(v, angle)
    else:
        raise

Prevention

When it happens

Trigger: horizontal_distance(50, 0); horizontal_distance(50, 95); horizontal_distance(50, -30); horizontal_distance(50, 90.5). The check is angle > 90 or angle < 1, so exactly 1 and exactly 90 are valid — 0 (purely horizontal) and anything past 90 raise.

Common situations: Angles computed modulo arithmetic that land at 0 or above 90; converting radians to degrees incorrectly (e.g. passing 1.57 radians directly); spherical-coordinate data where elevation can be negative or exceed 90.

Related errors


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