python - can't seem to get an output from my class -


i can't seem figure out wrong code , why not getting out. can't seem figure out how expand rectangle. instructions: create method called expand takes offset value , returns copy of rectangle expanded offset in directions.

>>> r = rectangle(30, 40, 100, 110) >>> print(r)        rectangle(30, 40, 100, 110)  >>> r1 = r.expand(offset=3)  >>> print(r1)    rectangle(27, 37, 106, 116)     

the original rectangle should not modified.

>>> print(r)    rectangle(30, 40, 100, 110) 

negative values should return shrunken rectangle.

>>> r2 = r.expand(-5)  >>> print(r2)    rectangle(35, 45, 90, 100) 

this have far:

class rectangle:      def __init__(self, initx, inity, initw, inith):          self.x = initx          self.y = inity          self.w = initw          self.h = inith      def __str__(self):         return('rectangle(' + str(self.x) + ',' + str(self.y) + ','                             + str(self.w) + ',' + str(self.h)+')')     def right(self):         return self.x + self.w       def top(self):         return self.y + self.h       def size(self):         return '(' + self.w + ',' + self.h+ ')'      def position(self):         return '(' + self.x + ',' + self.y + ')'      def area(self):        return self.w * self.h      def expand(self):     # can't figure part out       r = rectangle(5,10,50,100)     r2 = rectangle(5,10,50,100)     r3 = rectangle(3,5,10,20)     r4 = rectangle(12,10,72,35)     r5 = rectangle(5,7,10,6)     r6 = rectangle(1,2,3,4)       print(r2)      print(r3.right())      print(r4.right())      print(r5.top())      print(r6.size())      print(r6.position())      print(r6.area()) 

def expand(self,expand):     return rectangle(self.x - expand, self.y - expand, self.w + 2*expand,                        self.h + 2*expand) 

or more generic against future overload....

def expand(self,expand):     return type(self)(self.x - expand, self.y - expand, self.w + 2*expand,                        self.h + 2*expand) 

and .... negative dimensions? maybe better fix in constructor:

def __init__(self, initx, inity, initw, inith):     self.x = initx     self.y = inity     self.w = max(0,initw)     self.h = max(0,inith) 

finally can talking subject of question: can't seem output class. maybe indentation issue, i'll rewrite script methods , fixing indentation issue.

class rectangle:      def __init__(self, initx, inity, initw, inith):         self.x = initx         self.y = inity         self.w = max(0,initw)         self.h = max(0,inith)      def __str__(self):         return('rectangle(' + str(self.x) + ',' + str(self.y) + ','                             + str(self.w) + ',' + str(self.h)+')')     def right(self):         return self.x + self.w      def top(self):         return self.y + self.h      def size(self):         return '(' + self.w + ',' + self.h+ ')'      def position(self):         return '(' + self.x + ',' + self.y + ')'      def area(self):        return self.w * self.h      def expand(self,expand):        return type(self)(self.x - expand, self.y - expand, self.w + 2*expand,                        self.h + 2*expand)   r = rectangle(5,10,50,100) r2 = rectangle(5,10,50,100) r3 = rectangle(3,5,10,20) r4 = rectangle(12,10,72,35) r5 = rectangle(5,7,10,6) r6 = rectangle(1,2,3,4)  print(r2) print(r3.right()) print(r4.right()) print(r5.top()) print(r6.size()) print(r6.position()) print(r6.area()) 

now should work expect. in code had put variable definitions , print statements in class definition.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -