我試圖實現我的第一課,部分是爲了分解建模和解決數學問題的方法,我遇到了麻煩。我認爲我的問題不涉及班級,但是......?調用類中的函數來設置列表的元素
錯誤總是告訴我說:「NameError:全局名稱‘corner2’沒有定義」
我試圖移動函數調用,但它仍然不承認它,所以我把它放回列表聲明在我的init函數中。
這裏是我的代碼:
class RotatedRectangle(object):
def corner1(a,b):
a/=2
b/=2
x=(a-b)*math.sin(math.pi/4)
y=(a+b)*math.sin(math.pi/4)
return (x,y)
def corner2(a,b):
a/=-2
b/=2
x=(a-b)*math.sin(math.pi/4)
y=(a+b)*math.sin(math.pi/4)
return (x,y)
def corner3(a,b):
a/=-2
b/=-2
x=(a-b)*math.sin(math.pi/4)
y=(a+b)*math.sin(math.pi/4)
return (x,y)
def corner4(a,b):
a/=2
b/=2
x=(a-b)*math.sin(math.pi/4)
y=(a+b)*math.sin(math.pi/4)
return (x,y)
def __init__(self, a, b,):
"""Return a Rotated rectangle object whose name is a function of a and b."""
self.name = str(a) + "," + str(b) + "-rectangle"
self.corners = [corner1(a,b), corner2(a,b), corner3(a,b), corner4(a,b)]
"""A rectangle with sides equal to even integers a and b is drawn on the Cartesian plane.Its center (the intersection point of its diagonals) coincides with the point (0, 0),but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.
How many points with integer coordinates are located inside the given rectangle (including on its sides)? """
是啊,有一件事我不愛約蟒。複製和粘貼似乎一直導致縮進錯誤。儘管謝謝你的回覆。我會採取你的建議閱讀,似乎有這個類的一些東西,我仍然不明白或不夠熟悉。 – Carl
@Carl,你必須一步一步做。不要忘了把問題標記爲接受和投票,如果是有用的:) –