1

这发生在 IDLE 和 Windows 7 RC1 中(如果有帮助的话)。这是模块:

    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()

当我运行程序时,我收到错误:您的程序中有错误:语法无效。IDLE 然后突出显示饼函数中的“轮辐”字样。

如果我注释掉整个饼函数,程序运行完美。

4

4 回答 4

3

快速浏览一下,在调用spokes() 之前,您是否错过了行尾的右括号?

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

代替

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
于 2009-11-06T19:04:36.977 回答
2

上一行缺少右括号。它应该改为这样:

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2))
于 2009-11-06T19:03:27.023 回答
2

上一行缺少右括号:

length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
于 2009-11-06T19:03:27.450 回答
2

其他人已经指出了实际的语法错误,所以我不再多说。我要补充的一件事是,如果我遇到语法错误,我首先查看的是函数之前的行。通常它就像一个括号丢失或逗号在错误的地方。

于 2009-11-06T19:08:03.553 回答