On Feb 18, 4:16 pm, "Neil" <rubik_wiz...@no.spamhotmail.com> wrote: > Hello > > Sorry if this is not an appropriate newsgroup for this problem. I am very > new to Python but not new to programming. > > I am hoping to use Python to teach a class and have been looking at the > online book 'Snake Wrangling for Kids'. > > I have followed the example > > >>>import turtle > >>>t=turtle.pen()
You defined "t" to be a pen object, not a turtle object. > > which works fine and displays a turtle in a window. > > However whenever I try to use something like > > >>>t.forward(50) (taken from the book) Pens don't move, turtles move (and the pen the turtle is carrying draws a line, if down). > > i get the error... > > Traceback (most recent call last): > File "<pyshell#3>", line 1, in <module> > t.forward(50) > AttributeError: 'dict' object has no attribute 'forward' > > I get the same error with t.left(10) etc. etc. Pens can't turn left, turtles turn left. > > I have tried this with V2.6 and V3 > > Any ideas would be much appreciated. > > Thanks > Neil Here's an example. It draws a Minimal Spanning Tree. If you don't need an MST, take note of how I use the turtle. import turtle import random import copy def build_sv(start,stop,limit,C): """Sequence Vector Generator build_sv(start,stop,limit,C) start: start of Collatz sequence (must be odd) stop: sequence termination limit: max length of [sv] if stop not found limit is sum(sv), not len(sv) C: C of 3n+C returns [] if invalid returns SequenceVector [sv] """ ONE = 1 TWO = 2 TWE = 3 sv = [] if (start % 2)==0: return sv done = 0 count = 0 while done==0: # # oops, power division may skip past stopping point # without seeing it # start = TWE*start + C f = 0 t = start while t%2==0: f += 1 t /= 2 g = 0 # stopping point may occur before f is reached while (not done) and g<f: start = start >> 1 if start==stop: done = 1 count += 1 g += 1 if count==limit: done = 1 sv.append(g) return sv def Type12MH(k,i): """Find ith, kth Generation Type [1,2] Mersenne Hailstone using the closed form equation Type12MH(k,i) k: generation i: member of generation returns Hailstone (a) """ ONE = 1 TWO = 2 SIX = 6 NIN = 9 if (k<1) or (i<1): return 0 a = (i-ONE)*NIN**(k-ONE) + (NIN**(k-ONE) - ONE)/TWO + ONE return TWO**(SIX*a - ONE) - ONE turtle.clear() turtle.up() the_window = (turtle.window_width(),turtle.window_height()) print the_window, turtle.goto(0,0) tooter = turtle.position() buffer_L = -((the_window[0]-200)/2) buffer_R = (the_window[0]-200)/2 buffer_T = (the_window[1]-200)/2 buffer_B = -((the_window[1]-200)/2) print buffer_L,buffer_R,buffer_T,buffer_B turtle.tracer(False) turtle.down() turtle.speed('fastest') t = 0 turtle.setheading(t) for j in range(1): p = Type12MH(4,2) # Type12 Mersenne Hailstone (very big) sv = build_sv(p,1,10000000,1) # limit to 10 million numbers in vector # Minimal Spanning Tree stars = [] turtle.up() for d in sv: # random turtle crawl based on sv if d>1: # skip small sv values for movement for r in xrange(1): turtle.setheading(t+r*90) turtle.forward(d*4) # scale of grid tooter = turtle.position() if d>8: # create a star at really large sv values turtle.down() turtle.width(3) turtle.color('black') turtle.forward(1) # draw a blob turtle.backward(1) stars.append((int(tooter[0]),int(tooter[1]))) turtle.up() turtle.width(1) turtle.color('red') # color of MST spans whither_tooter = [] # build list of legal next turns if tooter[0]>buffer_L: # test for being too close to screen edge whither_tooter.append(180) if tooter[0]<buffer_R: whither_tooter.append(0) if tooter[1]<buffer_T: whither_tooter.append(90) if tooter[1]>buffer_B: whither_tooter.append(270) t = random.choice(whither_tooter) # choose random direction from list of legal moves print 'stars:',len(stars) star_dist = [] # create list of spans between stars for i,src in enumerate(stars): for j,dst in enumerate(stars): dist = turtle.sqrt((src[0]-dst[0])**2 + (src[1]-dst[1])**2) if dist == 0: star_dist.append([9999,i,j]) else: star_dist.append([dist,i,j]) star_dist.sort() MST = [] # spans of MST MST2 = [] # only those spans that can connect to existing MST points_used = {} # list of stars that are currently on MST s = copy.deepcopy(star_dist[0]) # going to modify list element, so deep copy needed MST.append(s) # fist span always smallest absolute distance points_used[s[1]] = 1 points_used[s[2]] = 1 star_dist[0][0] = 9999 # overwrite distance so this span can't be used again found = False # also, overwrite complement span si = 0 while not found: ss = star_dist[si] if s[1]==ss[2] and s[2]==ss[1]: # the complement star_dist[si][0] = 9999 found = True else: si += 1 while len(MST)<(len(stars)-1): # an MST of n points always has n-1 spans for i,sd in enumerate(star_dist): # copy points matching s to MST2 # only spans that could connect to existing # tree points can be added at this stage # (only have to check for last added to MST) if (sd[0]!=9999) and \ ((sd[1]==s[1]) or \ (sd[1]==s[2]) or \ (sd[2]==s[1]) or \ (sd[2]==s[2])): MST2.append(copy.deepcopy(sd)) star_dist[i][0] = 9999 # once copied to MST2, overwrite original distance MST2.sort() close = 0 # # can't use a span if BOTH points already on MST, otherwise we get loops # and some stars remain disjoint # while ((MST2[close][1] in points_used) and (MST2[close][2] in points_used)): close += 1 s = copy.deepcopy(MST2[close]) MST.append(s) if s[1] in points_used: # only keys used to create tree, values points_used[s[1]] += 1 # will eventully be number of points else: # connected to this point points_used[s[1]] = 1 if s[2] in points_used: points_used[s[2]] += 1 else: points_used[s[2]] = 1 MST2[close][0] = 9999 # overwrite this point and its complement found = False si = 0 while not found: ss = MST2[si] if s[1]==ss[2] and s[2]==ss[1]: # the duplicate MST2[si][0] = 9999 found = True else: si += 1 if si == len(MST2): found = True for span in MST: # from an MST span srcx = stars[span[1]][0] # get coords of two endpoints srcy = stars[span[1]][1] dstx = stars[span[2]][0] dsty = stars[span[2]][1] turtle.up() # and draw a line between them turtle.goto(srcx,srcy) turtle.down() turtle.goto(dstx,dsty) -- http://mail.python.org/mailman/listinfo/python-list