{"record":{"id":"6566e99ca199c27d","repo":"TheAlgorithms/Python","slug":"iterations-must-be-defined-as-integers","errorCode":null,"errorMessage":"iterations must be defined as integers","messagePattern":"iterations must be defined as integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/fizz_buzz.py","lineNumber":38,"sourceCode":"        ...\n    ValueError: starting number must be\n                             and integer and be more than 0\n    >>> fizz_buzz(10,-5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Iterations must be done more than 0 times to play FizzBuzz\n    >>> fizz_buzz(1.5,5)\n    Traceback (most recent call last):\n        ...\n    ValueError: starting number must be\n                             and integer and be more than 0\n    >>> fizz_buzz(1,5.5)\n    Traceback (most recent call last):\n        ...\n    ValueError: iterations must be defined as integers\n    \"\"\"\n    if not isinstance(iterations, int):\n        raise ValueError(\"iterations must be defined as integers\")\n    if not isinstance(number, int) or not number >= 1:\n        raise ValueError(\n            \"\"\"starting number must be\n                         and integer and be more than 0\"\"\"\n        )\n    if not iterations >= 1:\n        raise ValueError(\"Iterations must be done more than 0 times to play FizzBuzz\")\n\n    out = \"\"\n    while number <= iterations:\n        if number % 3 == 0:\n            out += \"Fizz\"\n        if number % 5 == 0:\n            out += \"Buzz\"\n        if 0 not in (number % 3, number % 5):\n            out += str(number)\n\n        # print(out)","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/fizz_buzz.py#L20-L56","documentation":"Raised by fizz_buzz(iterations, number) when the first parameter is not an int (isinstance(iterations, int) fails). Note bool is a subclass of int, so True/False pass; floats, strings, and None raise. It is the first of three sequential validation checks in the function.","triggerScenarios":"fizz_buzz(1, 5.5) as shown in the doctest (iterations=1 is fine, but symmetrically fizz_buzz('10', 1) raises here); passing a float iteration count like fizz_buzz(10.0, 1); passing None or a string parsed from CLI args without conversion.","commonSituations":"CLI or HTTP query parameters arriving as strings; argparse without type=int; JSON config values that deserialize as floats (10.0 vs 10).","solutions":["Convert the argument to int at the boundary: fizz_buzz(int(iterations), number).","Use argparse type=int or explicit schema validation for config/CLI input.","Check types before calling when data comes from untrusted sources."],"exampleFix":"# before\nfizz_buzz(request.args.get('n'), 1)  # str like '15' -> ValueError\n\n# after\nfizz_buzz(int(request.args.get('n')), 1)","handlingStrategy":"type-guard","validationCode":"if not isinstance(iterations, int) or isinstance(iterations, bool):\n    iterations = int(iterations)\nfizz_buzz(iterations, number)","typeGuard":"def is_plain_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool)","tryCatchPattern":"try:\n    out = fizz_buzz(iterations, number)\nexcept ValueError as exc:\n    if 'iterations must be defined as integers' in str(exc):\n        out = fizz_buzz(int(iterations), number)\n    else:\n        raise","preventionTips":["Convert CLI/query-string arguments with int() before passing to numeric functions.","Remember bool is an int subclass — exclude it when type-checking user data.","Keep JSON numbers as ints (avoid 10.0) by normalizing at deserialization."],"tags":["python","input-validation","type-error","dynamic-programming"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}