{"record":{"id":"7f6adcb3b7e1d02d","repo":"TheAlgorithms/Python","slug":"number-must-be-integer-and-greater-than-zero","errorCode":null,"errorMessage":"number must be integer and greater than zero","messagePattern":"number must be integer and greater than zero","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"maths/gcd_of_n_numbers.py","lineNumber":44,"sourceCode":"        ...\n    TypeError: number must be integer and greater than zero\n    >>> get_factors(1.5)\n    Traceback (most recent call last):\n        ...\n    TypeError: number must be integer and greater than zero\n\n    factor can be all numbers from 2 to number that we check if number % factor == 0\n    if it is equal to zero, we check again with number // factor\n    else we increase factor by one\n    \"\"\"\n\n    match number:\n        case int(number) if number == 1:\n            return Counter({1: 1})\n        case int(num) if number > 0:\n            number = num\n        case _:\n            raise TypeError(\"number must be integer and greater than zero\")\n\n    factors = factors or Counter()\n\n    if number == factor:  # break condition\n        # all numbers are factors of itself\n        factors[factor] += 1\n        return factors\n\n    if number % factor > 0:\n        # if it is greater than zero\n        # so it is not a factor of number and we check next number\n        return get_factors(number, factors, factor + 1)\n\n    factors[factor] += 1\n    # else we update factors (that is Counter(dict-like) type) and check again\n    return get_factors(number // factor, factors, factor)\n\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/gcd_of_n_numbers.py#L26-L62","documentation":"Raised by get_factors in maths/gcd_of_n_numbers.py when the input does not match a positive integer. The function uses a match statement: int 1 returns Counter({1:1}), positive ints proceed to trial-division factoring, and everything else (str, float, None, negative ints, bools that fail the guard, etc.) falls to the wildcard case raising TypeError. It exists because prime factorization is only defined for positive integers.","triggerScenarios":"Calling get_factors('12'), get_factors(3.5), get_factors(-4), get_factors(0), or get_factors(None). The wildcard 'case _' arm raises TypeError('number must be integer and greater than zero').","commonSituations":"Passing unvalidated user input (CLI args, form fields are strings) into gcd computation; negative numbers from subtraction logic reaching the factorizer; data pipelines mixing int and float representations of whole numbers.","solutions":["Convert and validate inputs before calling: use int(x) inside a try/except and check x > 0.","If calling via get_greatest_common_divisor, ensure every element of numbers is a positive int.","Reject or normalize float-typed whole numbers (e.g. 4.0 -> 4) at your API boundary."],"exampleFix":"// before\nfactors = get_factors(raw_input)  # raw_input may be '12' or -3\n\n// after\ntry:\n    n = int(raw_input)\nexcept (TypeError, ValueError) as e:\n    raise TypeError(f\"expected positive integer, got {raw_input!r}\") from e\nif n <= 0:\n    raise TypeError(f\"expected positive integer, got {n}\")\nfactors = get_factors(n)","handlingStrategy":"type-guard","validationCode":"def coerce_positive_int(value):\n    try:\n        n = int(value)\n    except (TypeError, ValueError) as e:\n        raise TypeError(f\"expected positive integer, got {value!r}\") from e\n    if isinstance(value, float) and value != n:\n        raise TypeError(f\"non-integer float: {value!r}\")\n    if n <= 0:\n        raise TypeError(f\"must be > 0, got {n}\")\n    return n","typeGuard":"def is_positive_int(value) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value > 0","tryCatchPattern":"try:\n    factors = get_factors(value)\nexcept TypeError as e:\n    # log and reject the bad value\n    ...","preventionTips":["Convert string inputs to int at the system boundary","Exclude 0 and negatives before factorization"],"tags":["math","gcd","factorization","typeerror","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}