1

This is the problem,

Define the class Rectangle. It's constructor takes a pair of numbers representing the top-left corner, and two other numbers representing the width and height. It has the following methods:

get_bottom_right() - return the bottom right corner as a pair of numbers. move(p) - move the rectangle so that p becomes the top-left corner (leaving the width and height unchanged). resize(width, height) - set the width and height of the rectangle to the supplied arguments (leaving the top-left corner unchanged). __str__() - return the string representation of the rectangle as a pair of pairs - i.e. the top left and bottom right corners.

Note: Unlike the co-ordinate system you may have encountered before, y, in this question, increases as you move vertically downwards.

Examples: Each example below follows on from the previous one. Be careful to get the spacing right for str - i.e. comma followed by space.

r = Rectangle((2,3), 5, 7)
str(r)
*'((2, 3), (7, 10))'*
r.move((5,6))
str(r)
'((5, 6), (10, 13))'
r.resize(2,3)
str(r)
*'((5,6), (7, 9))'*
r.get_bottom_right()
*(7, 9)*

This is my answer:

class Rectangle():
 def __init__(self, coords, sizex, sizey):
 self._startx, self._starty = coords
 self._sizex = sizex
 self._sizey = sizey
 def get_bottom_right(self):
 print '(%s, %s)' % (self._startx + self._sizex, self._starty + self._sizey)
 def move(self, pos):
 self._startx, self._starty = pos
 def resize(self, width, height):
 self._sizex = width
 self._sizey = height
 def __str__(self):
 return '((%s, %s), (%s, %s))' % (self._startx, self._starty, self._startx + self._sizex, self._starty + self._sizey)
r = Rectangle((2, 3), 5, 7)
str(r)
r.move((5,6))
str(r)
r.resize(2,3)
str(r)
r.get_bottom_right()

I got the correct answer but the homeowork system says im wrong.

Wrong: the Rectangle class should inherit from the object class

can anyone tell me where im wrong?

Artjom B.
62k26 gold badges137 silver badges236 bronze badges
asked Sep 7, 2014 at 8:49

2 Answers 2

2

You are looking for,

class Rectangle(object):
...

This is a new-style class. Read more in this SO post: What is the difference between old style and new style classes in Python?

I also have a feeling that your homework system is using pylint to verify/grade your code. You might want to stick to PEP8 standards.

answered Sep 7, 2014 at 8:50
Sign up to request clarification or add additional context in comments.

4 Comments

(object) is indeed the new style
I have changed to Rectangle(object), but why does my answer changes as well. It was (7,9) and it is (14,11) now. what is happening....
@bboychua Check if you've changed the numbers by mistake? There is no way inheriting object is affecting it.
okay this is what happened. if I use Rectangle(), I get (7,9), and if I use Rectangle(object), I get different answers everytime.
1

Another error in your Rectangle class is that the get_bottom_right() method is supposed to return a tuple, not print it. Of course, returning a tuple in the interpreter without assigning it will cause it to be printed.

answered Sep 7, 2014 at 9:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.