{"record":{"id":"38a7ea61bb7e0e0f","repo":"TheAlgorithms/Python","slug":"numbers-must-be-integer-and-greater-than-zero","errorCode":null,"errorMessage":"numbers must be integer and greater than zero","messagePattern":"numbers must be integer and greater than zero","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"maths/gcd_of_n_numbers.py","lineNumber":92,"sourceCode":"        ...\n    Exception: numbers must be integer and greater than zero\n    >>> get_greatest_common_divisor(1.5, 2)\n    Traceback (most recent call last):\n        ...\n    Exception: numbers must be integer and greater than zero\n    >>> get_greatest_common_divisor(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n    1\n    >>> get_greatest_common_divisor(\"1\", 2, 3, 4, 5, 6, 7, 8, 9, 10)\n    Traceback (most recent call last):\n        ...\n    Exception: numbers must be integer and greater than zero\n    \"\"\"\n\n    # we just need factors, not numbers itself\n    try:\n        same_factors, *factors = map(get_factors, numbers)\n    except TypeError as e:\n        raise Exception(\"numbers must be integer and greater than zero\") from e\n\n    for factor in factors:\n        same_factors &= factor\n        # get common factor between all\n        # `&` return common elements with smaller value (for Counter type)\n\n    # now, same_factors is something like {2: 2, 3: 4} that means 2 * 2 * 3 * 3 * 3 * 3\n    mult = 1\n    # power each factor and multiply\n    # for {2: 2, 3: 4}, it is [4, 81] and then 324\n    for m in [factor**power for factor, power in same_factors.items()]:\n        mult *= m\n    return mult\n\n\nif __name__ == \"__main__\":\n    print(get_greatest_common_divisor(18, 45))  # 9\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/gcd_of_n_numbers.py#L74-L110","documentation":"Raised by get_greatest_common_divisor in maths/gcd_of_n_numbers.py when any element of numbers fails factorization. The function maps get_factors over all inputs inside a try block; get_factors raises TypeError for non-positive-integers, and the except clause re-raises it as a generic Exception('numbers must be integer and greater than zero') chained from the original. One bad element aborts the whole GCD computation.","triggerScenarios":"Calling get_greatest_common_divisor('1', 2, 3) or with any single non-int, zero, or negative element among the arguments. The map(get_factors, numbers) call raises TypeError which is wrapped and re-raised.","commonSituations":"Spreads of mixed data (e.g. a list containing a string from parsing); forgetting that a 0 in the dataset is invalid for factorization; assuming the function skips bad values instead of failing fast.","solutions":["Sanitize the whole collection before calling: nums = [int(n) for n in numbers] and assert all n > 0.","Filter out invalid entries if they are expected noise: nums = [n for n in numbers if isinstance(n, int) and n > 0] (then require nums non-empty).","Catch Exception (the wrapper is generic, not TypeError) around the call if you must handle it, and inspect __cause__ for the original error."],"exampleFix":"// before\ngcd = get_greatest_common_divisor(*values)  # values may contain '1' or 0\n\n// after\nvalues = [int(v) for v in values]\nif any(v <= 0 for v in values):\n    raise ValueError(f\"all inputs must be positive integers: {values}\")\ngcd = get_greatest_common_divisor(*values)","handlingStrategy":"validation","validationCode":"nums = [int(n) for n in numbers]\nif not nums or any(n <= 0 for n in nums):\n    raise ValueError(f\"all inputs must be positive integers: {numbers!r}\")\ngcd = get_greatest_common_divisor(*nums)","typeGuard":"def all_positive_ints(seq) -> bool:\n    return bool(seq) and all(isinstance(n, int) and n > 0 for n in seq)","tryCatchPattern":"try:\n    g = get_greatest_common_divisor(*nums)\nexcept Exception as e:  # wrapper raises generic Exception, not TypeError\n    if isinstance(e.__cause__, TypeError):\n        # invalid element detected\n        ...","preventionTips":["Validate the whole collection, not each call","Remember the re-raise is a bare Exception, so catching TypeError will miss it"],"tags":["math","gcd","input-validation","exception-wrapping"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}