Why is turtle.done() saying it has no attribute 'done'?

Viewed 54

I'm using the turtle module. My goal is to keep the window open, and I have heard to use turtle.done() to do so. So far, I've always got this error message:

AttributeError: type object 'Turtle' has no attribute 'done'. Did you mean: 'clone'?

Here is my code.

import turtle
from turtle import Turtle

turtle = Turtle()

turtle.forward(100)
turtle.done()
3 Answers

You shouldn't have a variable name the same as a module. Change the object name from turtle to t, for example, and it'll work:

import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()

done is a function in the turtle module.

By writing turtle = Turtle() you overwrote the name turtle so that it no longer refers to the module, but to an instance of the class Turtle which does in fact not have a method called done.

The simplest solution would be to choose a different variable name for the Turtle instance.

done is a module-level function, not a method on the Turtle object. Import it from the turtle module on the same line where you import the Turtle class:

from turtle import Turtle, done

turtle = Turtle()
turtle.forward(100)

done()

or import the entire module as turtle (in which case you should not reuse the name turtle for one of your own variables):

import turtle

t = turtle.Turtle()
t.forward(100)

turtle.done()
Related