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 init_velocity is not an int or float. check_args is the shared validator called by horizontal_distance and related projectile functions; it enforces numeric types before any trigonometry runs. bool passes isinstance(x, (int, float)) since bool subclasses int, and strings, None, and Decimal all fail.
Source
Thrown at bit_manipulation/binary_count_setbits.py:32
>>> binary_count_setbits(4294967295)
32
>>> binary_count_setbits(0)
0
>>> binary_count_setbits(-10)
Traceback (most recent call last):
...
ValueError: Input value must be a positive integer
>>> binary_count_setbits(0.8)
Traceback (most recent call last):
...
TypeError: Input value must be a 'int' type
>>> binary_count_setbits("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 bin(a).count("1")
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Convert to float before calling: float(velocity_str).
- Check for None / missing optional values and supply a default or skip.
- Catch TypeError at input boundaries to re-prompt or reject the form field.
Example fix
# before
dist = horizontal_distance(input('v: '), 45) # str -> TypeError
# after
dist = horizontal_distance(float(input('v: ')), 45) Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(init_velocity, (int, float)) or isinstance(init_velocity, bool):
init_velocity = float(init_velocity) # or raise
dist = horizontal_distance(init_velocity, angle) Type guard
def is_numeric_velocity(v: object) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) Try / catch
try:
dist = horizontal_distance(v, angle)
except TypeError as e:
if "velocity" in str(e):
dist = horizontal_distance(float(v), angle)
else:
raise Prevention
- Coerce str inputs with float() at the boundary (CLI, forms, CSV).
- Handle None optionals before calling.
- bool counts as int in Python — reject it explicitly if it can occur.
When it happens
Trigger: horizontal_distance('50', 45) with a string velocity from input() or a web form; passing None when an optional velocity was not provided; passing a numpy float64 is fine (it subclasses float), but a Decimal or Fraction is not.
Common situations: Unconverted user input from CLI prompts or HTML forms; data loaded as strings from JSON/CSV where numbers were quoted; Optional[float] fields that are None flowing into the call.
Related errors
- Input value must be a 'int' type
- Expected a list of numbers as input, found {type(item).__nam
- Expected a list of numbers as input, found {type(point).__na
- Input must be an integer
- Initial orbit radius must be greater than zero.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c0584909fb695d35.
Report an issue: GitHub.