views:

280

answers:

4

This happens in IDLE and Windows 7 RC1 (if that helps). Here is the module:

    from math import *
from TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
bob.delay = 0.1

def polyline(turtle, length, n, angle):
    for i in range(n):
        fd(turtle, length)
        rt(turtle, angle)

def polygon(turtle, length, n):
    """ polygon uses a turtle to draw a polygon
        with n sides of the given length.
    """
    angle = 360.0/n
    polyline(turtle, length, n, angle)

def spokes(turtle, length_of_spoke, number_of_spokes):
    angle = 360.0/number_of_spokes
    for i in range(number_of_spokes):
       turtle.fd(length_of_spoke)
       turtle.pd
       turtle.bk(length_of_spoke)
       turtle.rt(angle)
       turtle.pu

def pie(turtle, length_of_side, number_of_sides):
    """pie uses a turtle to draw a polygon
        with sides of the given length and with the given
        number of sides.
    """
    angle = 360.0/number_of_sides
    length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
    spokes(turtle, length_of_spoke, number_of_sides)
    turtle.pd
    turtle.fd(length_of_spoke)
    turtle.lt(270-angle/2)
    polygon(turtle, length_of_side, number_of_sides)

spokes(bob, 30, 11)

wait_for_user()

When I run the program, I get the error: There is an error in your program: invalid syntax. IDLE then highlights the "spokes" word within the pie-function.

If I comment-out the whole pie-function, the program runs perfectly.

+2  A: 

The previous line is missing a closing parenthesis. It should read like this instead:

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2))
Ville Laurikari
Oops! Thanks! That line had worked before, but at one point I could not decide wether to put it within the pie or the spokes function. The last parentheses must have been lost with the back-and-forth cutting and pasting.
Geoffrey Van Wyk
+2  A: 

There is a closing parenthesis missing in the previous line:

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
Tim Pietzcker
+3  A: 

At quick glance are you missing a closing parenthesis at the end of the line before you call spokes()?

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2))

instead of

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
Sean Copenhaver
+2  A: 

Others have already pointed out the actual syntax error, so I won't say any more about that. One thing I will add though is that if I get a syntax error, the first place I look is on the lines before the function. Usually it's something like a paren missing or a comma in the wrong place.

Jason Baker
+1 Thanks, I learned something. It is really strange behaviour for a built-in syntax checker. In the spokes function, I just placed an opening parenthesis in the assignment statement angle = (360.0/number_of_spokes, without adding the closing parenthesis, just to test, and the syntax checker highlighted the colon at the end of the for statement header! If one did not know about this behaviour, you would be flabbergasted.
Geoffrey Van Wyk
I'm not sure what background you're coming from, but this actually isn't all that uncommon. And if you think about it, it kind of makes sense. After all, if the interpreter/compiler knew what you were trying to tell it, it wouldn't be giving a syntax error. :-)
Jason Baker
What I meant was the line that contains the error should be highlighted, instead of the next line.
Geoffrey Van Wyk