TheAlgorithms/Python · error · ValueError

Iterations must be done more than 0 times to play FizzBuzz

Error message

Iterations must be done more than 0 times to play FizzBuzz

What it means

Raised by fizz_buzz when iterations is an int but is less than 1 (not iterations >= 1). This is the third and last validation check, so it fires only after the type checks passed — e.g. fizz_buzz(0, 1) or fizz_buzz(-5, 1). The loop 'while number <= iterations' would otherwise never execute, and the library treats zero iterations as invalid usage.

Source

Thrown at dynamic_programming/fizz_buzz.py:45

    >>> 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)
        number += 1
        out += " "
    return out


if __name__ == "__main__":
    import doctest

View on GitHub (pinned to f5988cc097)

Solutions

  1. Skip the call when the count is zero: if iterations >= 1: fizz_buzz(iterations, 1).
  2. Fix the count computation so it cannot be zero when FizzBuzz output is required.
  3. If 0 means 'no output' in your domain, short-circuit to an empty string instead of calling.

Example fix

# before
out = fizz_buzz(len(items), 1)  # empty items -> 0 iterations -> ValueError

# after
out = fizz_buzz(len(items), 1) if items else ''
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(iterations, int) and iterations >= 1:
    out = fizz_buzz(iterations, number)
else:
    out = ''

Type guard

def is_positive_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool) and value >= 1

Try / catch

try:
    out = fizz_buzz(iterations, number)
except ValueError as exc:
    if 'more than 0 times' in str(exc):
        out = ''  # zero iterations -> empty output
    else:
        raise

Prevention

When it happens

Trigger: fizz_buzz(0, 1), fizz_buzz(-10, 1); iterations computed as a count that legitimately equals zero (empty range), e.g. fizz_buzz(len(data), 1) with data == [].

Common situations: Iteration counts derived from collection sizes that can be empty; config values of 0 meaning 'unlimited' in some systems but rejected here; negative counts from subtraction.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/349a29508f77450f. Report an issue: GitHub.