TheAlgorithms/Python · error · ValueError
iterations must be defined as integers
Error message
iterations must be defined as integers
What it means
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.
Source
Thrown at dynamic_programming/fizz_buzz.py:38
...
ValueError: starting number must be
and integer and be more than 0
>>> fizz_buzz(10,-5)
Traceback (most recent call last):
...
ValueError: Iterations must be done more than 0 times to play FizzBuzz
>>> fizz_buzz(1.5,5)
Traceback (most recent call last):
...
ValueError: starting number must be
and integer and be more than 0
>>> fizz_buzz(1,5.5)
Traceback (most recent call last):
...
ValueError: iterations must be defined as integers
"""
if not isinstance(iterations, int):
raise ValueError("iterations must be defined as integers")
if not isinstance(number, int) or not number >= 1:
raise ValueError(
"""starting number must be
and integer and be more than 0"""
)
if not iterations >= 1:
raise ValueError("Iterations must be done more than 0 times to play FizzBuzz")
out = ""
while number <= iterations:
if number % 3 == 0:
out += "Fizz"
if number % 5 == 0:
out += "Buzz"
if 0 not in (number % 3, number % 5):
out += str(number)
# print(out)View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
fizz_buzz(request.args.get('n'), 1) # str like '15' -> ValueError
# after
fizz_buzz(int(request.args.get('n')), 1) Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(iterations, int) or isinstance(iterations, bool):
iterations = int(iterations)
fizz_buzz(iterations, number) Type guard
def is_plain_int(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool) Try / catch
try:
out = fizz_buzz(iterations, number)
except ValueError as exc:
if 'iterations must be defined as integers' in str(exc):
out = fizz_buzz(int(iterations), number)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: CLI or HTTP query parameters arriving as strings; argparse without type=int; JSON config values that deserialize as floats (10.0 vs 10).
Related errors
- All weights must be integers but got weight of type {type(wt
- numbers must be an iterable of integers
- numbers must be an iterable of integers
- The parameter days should be a list of integers
- 'float' object cannot be interpreted as an integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6566e99ca199c27d.
Report an issue: GitHub.