from swampy.TurtleWorld import *
world = TurtleWorld
bob = Turtle()
def polyline(t, n, length, angle):
"""Draws n line segments.
t: Turtle object
n: number of line segments
length: length of each segment
angle: degrees between segments
"""
for i in range(n):
fd(t, length)
lt(t, angle)
def polygon(t, n, length):
"""Draws a polygon with n sides.
t: Turtle
n: number of sides
length: length of each side.
"""
angle = 360.0/n
polyline(t, n, length, angle)
polygon(bob, n=7, length=70)
The code you posted does not produce the error you have described. If I run your code verbatim, I get:
Traceback (most recent call last):
File "foo.py", line 26, in <module>
polygon(bob, n=7, length=70)
File "foo.py", line 24, in polygon
polyline(t, n, length, angle)
File "foo.py", line 14, in polyline
fd(t, length)
File "/home/lars/tmp/runtime/lib/python2.7/site-packages/swampy/TurtleWorld.py", line 185, in fd
if self.pen and self.world.exists:
AttributeError: 'NoneType' object has no attribute 'exists'
It looks like this is due to an error in your code. You have:
world = TurtleWorld
But swampy.world.TurtleWorld.TurtleWord
is a class and needs to be instantiated:
world = TurtleWorld()
With this change in place, the code runs correctly and produces:
You have initialized the world
wrongly and aren't calling class functions properly. In order to make it work, you need assign the world for your turtle, not just somewhere randomly, and call the fd
and lt
functions as t.fd(length)
and t.lt(angle)
. If you don't understand why you need to do it this way, you can try to read something like this
from swampy.TurtleWorld import *
bob = Turtle()
bob.world = TurtleWorld()
def polyline(t, n, length, angle):
"""Draws n line segments.
t: Turtle object
n: number of line segments
length: length of each segment
angle: degrees between segments
"""
for i in range(n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
"""Draws a polygon with n sides.
t: Turtle
n: number of sides
length: length of each side.
"""
angle = 360.0/n
polyline(t, n, length, angle)
polygon(bob, 7, 70)
EDIT: larsks's solution works too, I changed the order of initializing bob
and world
when copy-pasting it to my file, so it didn't work for me, but he is right in this. I guessed I need to assign the TurtleWorld
to turle since I was working directly with the source code for TurtleWord , where the initializing for Turtle() is like
def __init__(self, world=None):
Animal.__init__(self, world)
so I determined using TurtleWorld()
as an attribute of turtle and then directly working with member functions ( fd
and lt
) was better.
well for the n problem, i made na raw_input, that worked for me. You could try doing that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.