print problem

This is the place for queries that don't fit in any of the other categories.

print problem

Postby heimdall01 » Tue Apr 22, 2014 5:38 pm

Returns a boolean value depending on whether a_point is inside this rectangle or not. Points on edge are not considered being inside so function will return False. and also we want print like this print ("rectangle "+ str(Rectangle(Point(x,y),width,height))+" contains "+str(Point(a,b)))

our code is

Code: Select all
class Point:
    """ Point class represents x,y geometric coords. """
    def __init__(self, a=0, b=0):
        """ Create a new point at a, b """
        self.x = a
        self.y = b

    def __str__(self):
        return "P(" + str(self.x) + "," + str(self.y) + ")"

class Rectangle:
    """ A class to manufacture rectangle objects """

    def __init__(self, top_left, width, height):
        """ Initialize rectangle by specifying its top left corner and size"""
        self.top_left = top_left
        self.width = width
        self.height = height

    my_rect_data = [ (-2,4,3,8), (5,-10,12,12),(3,5,10,20) ]
    for (x,y,width,height) in my_rect_data:
        print ("P(",x,",",y,")","S(",width,"x",height,")")

    my_rect_data = [  (-2,4,3,8), (5,-10,12,12),(3,5,10,20) ]

    area1 = my_rect_data[0][2]*my_rect_data[0][3]
    area2 = my_rect_data[1][2]*my_rect_data[1][3]
    area3 = my_rect_data[2][2]*my_rect_data[2][3]

    ceng = [area1,area2,area3]
    print("the largest area is",max(ceng))

    def contains(self, a_point):
        my_point_data = [ (0,0), (1,1), (-2,-2), (-3,4), (-1,4) ]
        for (a,b) in my_point_data:
         for (x,y) in my_rect_data:
            r = Rectangle(Point(3,5),10,20)
            x1 = x + width
            y1 = y - height

        if (x1 > a and  x < a and y1 < b and y > b):
            return True


"""print ("rectangle "+ str(Rectangle(Point(x,y),width,height))+" contains "+str(Point(a,b)))
print ("rectangle "+ str(Rectangle(Point(x,y),width,height))+" doesn't contain "+str(Point(a,b)))"""
Last edited by Yoriz on Tue Apr 22, 2014 5:58 pm, edited 1 time in total.
Reason: First post lock
Posts: 1
Joined: Tue Apr 22, 2014 5:36 pm

Re: print problem

Postby Yoriz » Tue Apr 22, 2014 6:01 pm

Hi welcome to the forum.
please use code tags when posting code to preserve the indentation, when you have a problem with your code state what you want to happen , what actually happens and what exceptions occur.
Please read the 'new users read this' link in my signature for more information on forum usage.
New Users, Read This
Join the #python-forum IRC channel on!
User avatar
Posts: 1629
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: print problem

Postby Mekire » Wed Apr 23, 2014 4:09 am

You really need to learn string formatting.

Lines like this:
Code: Select all
print("rectangle "+ str(Rectangle(Point(x,y),width,height))+" doesn't contain "+str(Point(a,b)))
Are fine when you are just starting, but it isn't how we actually do things.

Consider the following (ignore the usage of Pygame; focus on the prints):
Code: Select all
import pygame as pg

rect = pg.Rect(-25, 0, 50, 50)
points = [(0,5), (7,4), (51.1,2.5)]

for point in points:
    if rect.collidepoint(point):
        print("{} contains {}.".format(rect, point))
        print("{} doesn't contain {}.".format(rect, point))
Code: Select all
<rect(-25, 0, 50, 50)> contains (0, 5).
<rect(-25, 0, 50, 50)> contains (7, 4).
<rect(-25, 0, 50, 50)> doesn't contain (51.1, 2.5).

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1706
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests