في بيثون 2.6.4، لماذا أحصل على خطأ في بناء الجملة للحصول على مكالمة دالة، والتي يتم تعريف الوظيفة وتعمل تماما بمفردها؟
-
18-09-2019 - |
سؤال
يحدث هذا في الخمول و 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()
عندما أقوم بتشغيل البرنامج، أحصل على الخطأ: هناك خطأ في البرنامج الخاص بك: بناء جملة غير صالح. ثم يبرز الخمول كلمة "المتحدث" في وظيفة فطيرة.
إذا قمت بالتعليق على وظيفة فطيرة كاملة، فإن البرنامج يعمل تماما.
المحلول
الخط السابق يفتقد قوس إغلاق. يجب أن تقرأ مثل هذا بدلا من ذلك:
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)
وقد أشار آخرون بالفعل إلى خطأ في بناء الجملة الفعلي، لذلك لن أقول أي المزيد عن ذلك. شيء واحد سوف أضيف على الرغم من أنه إذا تلقيت خطأ في بناء الجملة، فإن المكان الأول الذي أبدو هو على الخطوط قبل وظيفة. عادة ما يكون شيء مثل خجن مفقود أو بفاصلة في المكان الخطأ.