2017-07-04 25 views
1

我試圖實現我的第一課,部分是爲了分解建模和解決數學問題的方法,我遇到了麻煩。我認爲我的問題不涉及班級,但是......?調用類中的函數來設置列表的元素

錯誤總是告訴我說:「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)? """ 

回答

1
  • 你必須使用自己作爲您的類方法
  • 致電時類方法的第一個參數在你的課堂內部,你必須傳遞自己作爲他們的第一個參數(以便班級知道你正在談論當前對象)
  • 在類中定義方法時必須縮進

    import math 
    class RotatedRectangle(object): 
        def corner1(self,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(self,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(self,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(self,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 = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)] 
    
1

你有幾個identation錯誤,你忘記添加self參數,它應該是:

import math 

class RotatedRectangle(object): 
    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 = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)] 

    def corner1(self,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(self,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(self,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(self,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) 

我強烈向你推薦閱讀這個問題和所有答案What is the purpose of self?

+0

是啊,有一件事我不愛約蟒。複製和粘貼似乎一直導致縮進錯誤。儘管謝謝你的回覆。我會採取你的建議閱讀,似乎有這個類的一些東西,我仍然不明白或不夠熟悉。 – Carl

+0

@Carl,你必須一步一步做。不要忘了把問題標記爲接受和投票,如果是有用的:) –

2

當在Python中爲類定義方法時,第一個參數通常設置爲「self」。然後,調用該方法時,與self.

這裏前綴是工作代碼:

import math 

class RotatedRectangle(object): 

    def corner1(self,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(self,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(self,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(self,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 = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)] 
+0

我認爲這就是問題,再加上一個:) –