0
class Rectangle(object):

def __init__(self, (top left corner), width, height):
    """
    __init__(self, (x, y), integer, integer)
    """

    self._x = x
    self._y = y
    self._width = width
    self._height = height

def get_bottom_right(self):
    x + self.width = d
    y + self.height = t

return '' + d,t

so i am trying to make a class for a rectangle, i am trying to find the bottom right of the rectangle. the bottom right of the rectangle can be found by adding the height and width onto the top left corner. eg. (2,3),4,7 would make the bottom corner be (6,10). however, i dont believe my code is right. this is my first time using classes so some hints and tricks on how to interpret this would be very helpful.

2
  • When you post code, you could make sure it's formatted correctly, since it matters in python. Commented Apr 16, 2012 at 2:02
  • sorry, i'll keep that in mind next time i post. Commented Apr 16, 2012 at 2:19

3 Answers 3

4

I think what you want is this

class Rectangle(object):
  def __init__(self, top_corner, width, height):
    self._x = top_corner[0]
    self._y = top_corner[1]
    self._width = width
    self._height = height

  def get_bottom_right(self):
    d = self._x + self.width
    t = self._y + self.height
    return (d,t)

You can use this like this

# Makes a rectangle at (2, 4) with width
# 6 and height 10
rect = new Rectangle((2, 4), 6, 10) 

# Returns (8, 14)
bottom_right = rect.get_bottom_right

Also, you could probably save yourself some time by making a Point class

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, this is what i wanted. i didn't know how to define top_corner with x and y.
1
class Rectangle(object):
  def __init__(self, pos, width, height):
    self._x = pos[0]
    self._y = pos[1]
    self._width = width
    self._height = height
  def get_bottom_right(self):
    d = self._x + self._width
    t = self._y + self._height
    return d,t

The code is running here: http://codepad.org/VfqMfXrt

1 Comment

i want to find the bottom of the rectangle. basically i want to add the topleftcorner(which will be x,y) and add the width to x and the height to y. thanks for your reply though
0
class BlockPuzzle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.grid = [[' ' for _ in range(width)] for _ in range(height)]
        self.colors = ['R', 'G', 'B', 'Y', 'O', 'P']  # Red, Green, Blue, Yellow, Orange, Purple

    def display(self):
        for row in self.grid:
            print(' | '.join(row))
        print("\n")

    def place_block(self, color, x, y):
        if 0 <= x < self.width and 0 <= y < self.height:
            if self.grid[y][x] == ' ':
                self.grid[y][x] = color
            else:
                print("Spot already taken!")

    def run_game(self):
        self.display()
        while True:
            color = input("Enter color (R, G, B, Y, O, P) or 'exit' to quit: ").upper()
            if color == 'EXIT':
                break
            if color not in self.colors:
                print("Invalid color!")
                continue
            x = int(input("Enter x position (0 to {}): ".format(self.width - 1)))
            y = int(input("Enter y position (0 to {}): ".format(self.height - 1)))
            self.place_block(color, x, y)
            self.display()

# Start the game
game = BlockPuzzle(5, 5)
game.run_game()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

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.