TheAlgorithms/Python · error · ValueError

starting number must be and integer

Error message

starting number must be
                         and integer and be more than 0

What it means

Raised by fizz_buzz when the second parameter, the starting number, is either not an int or is less than 1. The check is isinstance(number, int) or not number >= 1, so fizz_buzz(10, 0), fizz_buzz(10, -3), and fizz_buzz(10, 2.5) all raise. The message text is a triple-quoted string with embedded indentation ('and integer' typo included), so the raised message spans multiple lines.

Source

Thrown at dynamic_programming/fizz_buzz.py:40

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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Confirm argument order: fizz_buzz(iterations, number) — total iterations first, then start >= 1.
  2. Pass an int start value of at least 1: fizz_buzz(100, 1) for classic FizzBuzz.
  3. Coerce untrusted input: number = int(number) with a >= 1 check before the call.

Example fix

# before
fizz_buzz(1, 100)  # swapped args: start=100 ok but iterations=1; fizz_buzz(100, 0) -> ValueError

# after
fizz_buzz(100, 1)  # 100 iterations starting at 1
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(number, int) or number < 1:
    raise ValueError('starting number must be an integer >= 1')
out = fizz_buzz(iterations, number)

Type guard

def is_valid_start(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 'starting number' in str(exc):
        out = fizz_buzz(iterations, 1)  # restart from 1
    else:
        raise

Prevention

When it happens

Trigger: fizz_buzz(10, 0) or any start < 1; fizz_buzz(10, '1') or fizz_buzz(10, 1.0) with a non-int type; note the parameter order is (iterations, number), so swapped arguments like fizz_buzz(1, 10) meaning 'start at 1, 10 iterations' actually raise here.

Common situations: Confusing the parameter order (iterations first, starting number second); zero-based loop counters passed as start values; string values from user input not converted to int.

Related errors


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