{"record":{"id":"30d2d6402dabd182","repo":"TheAlgorithms/Python","slug":"additive-persistence-only-accepts-integral-value","errorCode":null,"errorMessage":"additive_persistence() only accepts integral values","messagePattern":"additive_persistence\\(\\) only accepts integral values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/persistence.py","lineNumber":59,"sourceCode":"    \"\"\"\n    Return the persistence of a given number.\n\n    https://en.wikipedia.org/wiki/Persistence_of_a_number\n\n    >>> additive_persistence(199)\n    3\n    >>> additive_persistence(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: additive_persistence() does not accept negative values\n    >>> additive_persistence(\"long number\")\n    Traceback (most recent call last):\n        ...\n    ValueError: additive_persistence() only accepts integral values\n    \"\"\"\n\n    if not isinstance(num, int):\n        raise ValueError(\"additive_persistence() only accepts integral values\")\n    if num < 0:\n        raise ValueError(\"additive_persistence() does not accept negative values\")\n\n    steps = 0\n    num_string = str(num)\n\n    while len(num_string) != 1:\n        numbers = [int(i) for i in num_string]\n\n        total = 0\n        for i in range(len(numbers)):\n            total += numbers[i]\n\n        num_string = str(total)\n\n        steps += 1\n    return steps\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/persistence.py#L41-L77","documentation":"additive_persistence() in maths/persistence.py counts how many times you must sum a number's digits until one digit remains. Like its multiplicative sibling, it first requires isinstance(num, int); strings, floats, and other types raise ValueError('additive_persistence() only accepts integral values'). The type check precedes the sign check, so any non-int input — including negative floats — triggers this error rather than the negative-values error.","triggerScenarios":"Calling additive_persistence('long number'), additive_persistence(123.0), or passing a value straight from input()/a deserialized payload. Booleans pass (bool subclasses int).","commonSituations":"Forwarding raw form/query-parameter strings into math helpers; refactoring older code that used len(str(num)) on flexible types; catching TypeError while the function actually raises ValueError.","solutions":["Convert the argument first: additive_persistence(int(num)).","Parse and type-check data at ingestion (API edge, file read) so math helpers always receive ints.","Catch ValueError, not TypeError, around these calls."],"exampleFix":"# before\nadditive_persistence(raw)  # ValueError when raw is '1234'\n\n# after\nadditive_persistence(int(raw))","handlingStrategy":"type-guard","validationCode":"if not isinstance(num, int):\n    num = int(num)\nadditive_persistence(num)","typeGuard":"def is_strict_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    additive_persistence(num)\nexcept ValueError as exc:\n    if 'integral' in str(exc):\n        num = int(float(num))\n    else:\n        raise","preventionTips":["Convert once at the edge; keep helpers strictly typed.","Catch ValueError — both guards in this file raise it.","Add type hints on your own wrappers to surface mismatches early."],"tags":["python","value-error","input-validation","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}