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()
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
How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle
from turtle import *
s = turtle()
why I am not getting any output? Error is occurring, saying turtle is not defined? It's python 3.7
s = turtle() statement is meant to create object of class turtle. But in module turtle there is no class by this name. That's why you're getting error NameError: name 'turtle' is not defined.
Turtle module is meant for simple drawings. You don't need to create any object (of any class) to start. Just call any drawing function straight away (and read module docs), for example:
from turtle import *
circle(50)
done()
More complex example taken from module docs:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
will visualize this picture:
I am trying to draw squares using turtle in python, and every time I want to command it to do something I must write turtle.
import turtle
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(200)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)
turtle.exitonclick()
I expect to write my code without having to write turtle every time
You can import everything from the turtle module by writing
from turtle import * # this is a wildcard import
Instead, however, you should just import turtle as t (or whatever else you want), like so:
import turtle as t # you can replace t with any valid variable name
Because wildcard imports tend to create function definition conflictions
Conversely, you could import only the classes (or methods) you need from from the module. Turtle is a necessary import:
from turtle import Turtle
Now, we have to instantiate it:
t = Turtle()
Now we can use it:
t.do_something() # methods that act on the turtle, like forward and backward
That will not import the Screen module however, so you won't be able to use exitonclick() unless you import Screen too:
from turtle import Turtle, Screen
s = Screen()
s.exitonclick()
As #cdlane notes though, loops may actually be your best bet for reducing the amount of code you have to write. This code:
for _ in range(x):
turtle.forward(100)
turtle.right(90)
Moves the turtle forwards then rightwards x times.
Do you have any idea how many comments there are on SO saying "Don't use wildcard imports" in response to people doing from turtle import * as folks are suggesting? I'll further argue, don't do import turtle as t as it exposes the functional interface to turtle. The turtle module is object-oriented, you need only expose that interface. If you're tired of typing so much, learn about loops:
from turtle import Screen, Turtle
t = Turtle()
for _ in range(4):
t.forward(100)
t.right(90)
for _ in range(4):
t.backward(100)
t.left(90)
t.backward(100)
for _ in range(3):
t.backward(100)
t.left(90)
s = Screen()
s.exitonclick()
Admittedly, I don't really mind wildcard imports for short turtle & tkinter examples as well as Zelle graphics programs. But none of that fd() nonsense instead of forward() either! Celebrate being a turtle, don't hide in your shell!
You could use the wildcard import:
from turtle import *
But it'd be better to use the prefixed imports to keep your namespaces clean. See #alec_a's answer.
Disclaimer: this answer is for lazy people like me :)
There are already good answers that show you how to solve your problem, and that warn you regarding wildcard imports.
If you just want to play with turtle module you can make your life easy, i.e. instead of writing turtle.forward(90) it is better to just write forward(90) but it will be super easy if you just write f(90)
again it will affect the readability of your code but common it deserve a trial
now your code will looks like
Edit: modify imports in one line as suggested by #chepner to be super lazier
from turtle import forward as f, back as b, right as r, left as l, exitonclick
# to draw one square
f(100)
r(90)
f(100)
r(90)
f(100)
r(90)
f(100)
exitonclick()
How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle
I want to write a turtle program where the turtle goes to wherever you click. So far I have this:
from turtle import *
screen = Screen()
turtle = Turtle()
screen.onscreenclick(turtle.goto)
But the problem is the turtle object just stays facing in the same direction. I want to somehow make it look towards where it's going. How can I achieve this?
This will do what you describe:
import turtle
screen = turtle.Screen()
turtle = turtle.Turtle()
def turtle_headto(x, y):
turtle.left(turtle.towards(x, y) - turtle.heading())
turtle.goto(x, y)
screen.onscreenclick(turtle_headto)
screen.mainloop()
But the motion of the arrow/turtle isn't always optimal, i.e. sometimes it spins the long way 'round, but that's something for you to optimize (e.g. when to call left() and when to call right())