在Python2.6.4中,为什么函数调用会出现语法错误,而函数是定义的,并且可以自己完美地工作?
这种情况发生在IDLE和Windows7RC1中(如果有帮助的话)。以下是模块:在Python2.6.4中,为什么函数调用会出现语法错误,而函数是定义的,并且可以自己完美地工作?,python,syntax,python-idle,Python,Syntax,Python Idle,这种情况发生在IDLE和Windows7RC1中(如果有帮助的话)。以下是模块: 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(turt
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然后突出显示饼图函数中的“辐条”字
如果我注释掉整个饼图函数,则程序运行正常。前一行缺少右括号。它应该是这样写的:
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2))
前一行缺少右括号。它应该是这样写的:
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2))
前一行中缺少右括号:
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
前一行中缺少右括号:
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
在调用spokes()之前,您是否在行的末尾缺少一个右括号 而不是
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
在调用spokes()之前,您是否在行的末尾缺少一个右括号 而不是
length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)
其他人已经指出了实际的语法错误,所以我不再赘述。不过,我要补充的一点是,如果出现语法错误,我首先会查看函数前面的行。通常是缺少一个paren或者逗号放错了地方。其他人已经指出了实际的语法错误,所以我不再多说了。不过,我要补充的一点是,如果出现语法错误,我首先会查看函数前面的行。通常是缺少一个paren或逗号放错了位置。Oops!谢谢这条线以前是有效的,但在某一点上我无法决定是把它放在饼函数还是辐条函数中。最后一个括号肯定是在来回剪切和粘贴时丢失的。哎呀!谢谢这条线以前是有效的,但在某一点上我无法决定是把它放在饼函数还是辐条函数中。最后的括号肯定是在来回剪切和粘贴过程中丢失的。+1谢谢,我学到了一些东西。对于内置语法检查器来说,这是一种非常奇怪的行为。在spokes函数中,我在赋值语句angle=(360.0/number_of_spokes,不添加右括号,只是为了测试,语法检查器突出显示了for语句头末尾的冒号!如果不知道这种行为,你会大吃一惊。我不确定你来自什么背景,但这其实并不少见。如果你仔细想想毕竟,如果解释器/编译器知道您试图告诉它什么,它不会给出语法错误。:-)我的意思是,应该突出显示包含错误的那一行,而不是下一行。+1谢谢,我学到了一些东西。对于内置语法检查器来说,这确实是一种奇怪的行为。在spokes函数中,我只是在赋值语句angle=(360.0/number_of_spokes,不添加右括号,只是为了测试,语法检查器突出显示了for语句头末尾的冒号!如果不知道这种行为,你会大吃一惊。我不确定你来自什么背景,但这其实并不少见。如果你仔细想想它,这有点道理。毕竟,如果解释器/编译器知道您试图告诉它什么,它不会给出语法错误。:-)我的意思是应该突出显示包含错误的行,而不是下一行。