{"record":{"id":"c0584909fb695d35","repo":"TheAlgorithms/Python","slug":"input-value-must-be-a-positive-integer","errorCode":null,"errorMessage":"Input value must be a positive integer","messagePattern":"Input value must be a positive integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/binary_count_setbits.py","lineNumber":32,"sourceCode":"    >>> binary_count_setbits(4294967295)\n    32\n    >>> binary_count_setbits(0)\n    0\n    >>> binary_count_setbits(-10)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input value must be a positive integer\n    >>> binary_count_setbits(0.8)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value must be a 'int' type\n    >>> binary_count_setbits(\"0\")\n    Traceback (most recent call last):\n        ...\n    TypeError: '<' not supported between instances of 'str' and 'int'\n    \"\"\"\n    if a < 0:\n        raise ValueError(\"Input value must be a positive integer\")\n    elif isinstance(a, float):\n        raise TypeError(\"Input value must be a 'int' type\")\n    return bin(a).count(\"1\")\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":14,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/binary_count_setbits.py#L14-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ndist = horizontal_distance(input('v: '), 45)  # str -> TypeError\n\n# after\ndist = horizontal_distance(float(input('v: ')), 45)","handlingStrategy":"type-guard","validationCode":"if not isinstance(init_velocity, (int, float)) or isinstance(init_velocity, bool):\n    init_velocity = float(init_velocity)  # or raise\ndist = horizontal_distance(init_velocity, angle)","typeGuard":"def is_numeric_velocity(v: object) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool)","tryCatchPattern":"try:\n    dist = horizontal_distance(v, angle)\nexcept TypeError as e:\n    if \"velocity\" in str(e):\n        dist = horizontal_distance(float(v), angle)\n    else:\n        raise","preventionTips":["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."],"tags":["physics","typeerror","type-validation","user-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}