TheAlgorithms/Python · warning · ValueError

Correct format for using this script: python fractals.py <in

Error message

Correct format for using this script: python fractals.py <int:depth_for_fractal>

What it means

Raised by the __main__ block of fractals/sierpinski_triangle.py when sys.argv does not contain exactly one argument. The script then draws the Sierpinski triangle with turtle graphics at the given recursion depth. Note the message references 'fractals.py' although the file is sierpinski_triangle.py — a copy-paste leftover that makes the usage string misleading.

Source

Thrown at fractals/sierpinski_triangle.py:75

    """
    my_pen.up()
    my_pen.goto(vertex1[0], vertex1[1])
    my_pen.down()
    my_pen.goto(vertex2[0], vertex2[1])
    my_pen.goto(vertex3[0], vertex3[1])
    my_pen.goto(vertex1[0], vertex1[1])

    if depth == 0:
        return

    triangle(vertex1, get_mid(vertex1, vertex2), get_mid(vertex1, vertex3), depth - 1)
    triangle(vertex2, get_mid(vertex1, vertex2), get_mid(vertex2, vertex3), depth - 1)
    triangle(vertex3, get_mid(vertex3, vertex2), get_mid(vertex1, vertex3), depth - 1)


if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise ValueError(
            "Correct format for using this script: "
            "python fractals.py <int:depth_for_fractal>"
        )
    my_pen = turtle.Turtle()
    my_pen.ht()
    my_pen.speed(5)
    my_pen.pencolor("red")

    vertices = [(-175, -125), (0, 175), (175, -125)]  # vertices of triangle
    triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))
    turtle.Screen().exitonclick()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Run with exactly one integer argument: python sierpinski_triangle.py 3.
  2. If you maintain this script, fix the message to name the real file (or switch to argparse).
  3. Note this only guards the CLI path — importing triangle() directly needs no argv.

Example fix

# before (maintainer fix)
raise ValueError(
    "Correct format for using this script: "
    "python fractals.py <int:depth_for_fractal>"
)

# after
raise SystemExit(
    "Usage: python sierpinski_triangle.py <int:depth>"
)
Defensive patterns

Strategy: validation

Validate before calling

if len(sys.argv) != 2:
    raise SystemExit('Usage: python sierpinski_triangle.py <int:depth>')
try:
    depth = int(sys.argv[1])
except ValueError:
    raise SystemExit(f'depth must be an integer, got {sys.argv[1]!r}')

Prevention

When it happens

Trigger: Running python sierpinski_triangle.py (no depth), python sierpinski_triangle.py 3 4 (two args), or passing flags like --depth 3 (two args after the flag). Note a non-integer depth such as 'abc' passes the argc check and instead crashes at int(sys.argv[1]) with a different ValueError.

Common situations: Users expecting --help or argparse-style flags, running the module with extra arguments, or following the outdated usage text and getting confused by the wrong filename in the message.

Related errors


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