Issue
I'm trying Turtle for the first time and running into some trouble. I'm using ipyturtle, a widget that lets you use Turtle inline on a Jupyter notebook. It seems to be missing some commands. For example:
from ipyturtle import Turtle
t = Turtle()
t
size = 10
angle = 20
t.reset()
for a in range(10):
for i in range(100):
t.right(1)
t.forward(i/size)
t.home()
t.right(a*angle)
draws the first line, then throws the error:
AttributeError: 'Turtle' object has no attribute 'home'
It also seems to be missing goto()
, speed()
, among other key commands. Am I doing something wrong? If it is missing commands, how can you tell? I've used Python a fair amount in an engineering context but am unfamiliar with Github. I'd really appreciate an explanation of how someone navigating the page I linked above might sniff out a list of available commands.
I've tried running the following very similar block of code on Repl.it and it works fine:
from turtle import Turtle
t = Turtle()
size = 15
angle = 20
for a in range(1, 19):
for i in range(100):
t.right(1)
t.forward(i/size)
t.home()
t.right(a*angle)
Thanks in advance for your help!
Solution
Looking at the ipyturtle code, these are the turle methods supported:
position(self)
forward(self, length)
back(self, length)
heading(self)
goto(self, x, y=None)
setpos(self, x, y=None)
setposition(self, x, y=None)
left(self, degree=None)
right(self, degree=None)
penup(self)
pendown(self)
isdown(self)
hideturtle(self)
showturtle(self)
isvisible(self)
reset(self)
pencolor(self,r=-1,g=-1,b=-1)
So you're right about home()
and speed()
but goto()
does appear to be there. There also appears to be only one name for each command, not the wealth of aliases available in Python turtle (e.g. forward()
, fd()
).
The t.home()
call can be replaced by:
t.goto(0, 0)
t.setheading(0)
But in your example, you immediately do right()
afterward so we can combine that into the setheading()
. I believe the following should work in ipyturtle, Repl.it and standard Python:
from turtle import Turtle
size = 10
angle = 20
t = Turtle()
for a in range(1, 19):
for i in range(100):
t.right(1)
t.forward(i / size)
t.goto(0, 0)
t.setheading(-a * angle)
Answered By - cdlane
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.