2025, Dec 15 11:00

Why Your Python Turtle Window Closes After Input and How Converting to float() Fixes It

Python Turtle window closes immediately after input? Learn the cause (string used in math), how to convert input to float() and prevent silent errors.

Python scripts that open a Turtle graphics window can appear to “flash and disappear” when launched by double-click, especially right after reading user input. If the window closes immediately after you enter a value, it’s a strong sign the program hit an error and terminated before you could see what happened.

Reproducible snippet that triggers the issue

The following example draws a circle via Turtle. The behavior looks fine in some environments but the window closes instantly when started by double-click.

import math
import turtle
def draw_arc(tool, radius, deg):
    arc_len = 2 * math.pi * radius * deg / 360
    steps = int(arc_len / 3) + 1
    step_len = arc_len / steps
    step_deg = float(deg) / steps
    draw_polyline(tool, steps, step_len, step_deg)
def draw_polyline(tool, count, seg_len, turn_deg):
    for _ in range(count):
        tool.fd(seg_len)
        tool.lt(turn_deg)
def draw_circle(tool, r):
    draw_arc(tool, r, 360)
    turtle.mainloop()
user_radius = input("Enter the radius of the circle to be printed: ")
pen = turtle.Turtle()
draw_circle(pen, user_radius)

What’s really happening

The value stored in the radius variable is a string. That string is passed into the computation of arc_len inside draw_arc. When the program tries to evaluate the arithmetic expression that involves this string, an error occurs. Execution stops and, when the script is launched by double-click, the window closes with no visible traceback.

Launching the execution by hand in a shell allows you to see the error message.

Fix: convert the input to a numeric type

The minimal change is to convert the input to a floating-point number before using it in math. Everything else remains the same.

import math
import turtle
def draw_arc(tool, radius, deg):
    arc_len = 2 * math.pi * radius * deg / 360
    steps = int(arc_len / 3) + 1
    step_len = arc_len / steps
    step_deg = float(deg) / steps
    draw_polyline(tool, steps, step_len, step_deg)
def draw_polyline(tool, count, seg_len, turn_deg):
    for _ in range(count):
        tool.fd(seg_len)
        tool.lt(turn_deg)
def draw_circle(tool, r):
    draw_arc(tool, r, 360)
    turtle.mainloop()
user_radius = float(input("Enter the radius of the circle to be printed: "))
pen = turtle.Turtle()
draw_circle(pen, user_radius)

Why this detail matters

In short scripts that rely on GUI windows, any unhandled error can terminate the program so fast that you never see a message. If the value used in a numeric expression is a string, the computation fails, the program ends, and the Turtle window disappears. Converting the input ensures the math works and the window persists as intended.

Practical takeaways

When a Turtle window closes immediately after input, suspect an error that you can surface by running the script from a terminal. For numeric input, convert it to a number before using it in calculations. This simple habit prevents silent failures and saves time when working with small graphical utilities.