Python Class Runtime-Error -


I have just started Python and created a program about 8 line code which only calculates the area of ​​triangle , But whenever I try to turn it on, in area length = self.num1 attribute error in 'Area (1212) line 3', I get this error

file "", Line 1, 'triangle' object has no attribute 'num1'

and here is my code

  class tr Piedagon: def region (self, num1, num2): length = self. Num1 width = self.num2 field = (length * width) / 2 field = int (field) print ("your area is:% s"% area)  

help would be appreciated < /p>

As Message: Your object attribute num1 (and also an attribute num2 ) is not there. You need to set them somewhere in your class, that is,

  square triangle: def __init __ (self, number1, number 2): # length and width of triangle itself = length = num1 Self Width = num2 def area (self): # and all other stuff are here ...  

On the other hand, your method looks like you want to pass two values ​​ Num1 and num2 . In that case, assuming the value you have "code". needs to be removed, because you are giving values ​​as arguments:

  square triangle: def area (self, number 1, number 2): length = num1 width = Num2 # and all other stuff here ...  

Of course, you can num1 and num2 directly in this case:

  square triangle: def region (self, length, width): # and all other stuff here ...  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -