Okay learning python is somewhat hard.

Lamonte Harris pyth0nc0d3r at gmail.com
Tue Aug 28 18:21:29 EDT 2007


Yep I'll keep hacking at it aye :D.  Also I'll do some searching on the new
styled classes.

On 8/28/07, J. Cliff Dyer <jcd at sdf.lonestar.org> wrote:
>
> A couple thoughts:
>
> First, your class looks like it describes the line, not just the
> midpoint.  Your method calculates the midpoint of that line, so let the
> class be a line class.  That way you can add other useful methods to it:
>
> Line.midpoint()
> Line.length()
> Line.slope()
>
> etc.
>
> Second, you're losing fractions because all of your code deals with
> integers.  Integer division rounds down.  So instead of dividing by 2,
> divide by 2.0, to force the calculation into floating point.  Better yet
> (conceptually speaking), explicitly cast your input to floating point
> variables:  self.x1 = float(x1), etc.
>
> Third.  ALWAYS use new style classes.
>
> class Line(object):
>
> not
>
> class Line:
>
> Otherwise, it looks like you're on your way.  Keep hacking at it.
>
> Cheers,
> Cliff
>
>
>
> Lamonte Harris wrote:
> > Since I can't really focus on PROJECTS[since I don't know much python
> > I can't do any projects because all my projects are BIG], So I decided
> > to work on some problems I've gotten from school from started
> > geometry, So I attempted to make a script to get the midpoint of a
> > line segment using the formula giving:
> >
> > [1 and 2 are subscripts]
> >
> > X1 + X2
> > ----------- = mid pt. (x)
> >      2
> >
> > Y1 + Y2
> > ----------- = mid pt. (y)
> >      2
> >
> > So i tried coding the following script.
> >
> > #MIDPOINT OF A LINE
> > class midpoint:
> >     def __init__(self,x1,x2,y1,y2):
> >         self.x1 = x1
> >         self.x2 = x2
> >         self.y1 = y1
> >         self.y2 = y2
> >         self.dictionary = {'x' : '','y' : ''}
> >         self.calculate_mid_point()
> >     def calculate_mid_point(self):
> >         X = self.x1 + self.x2
> >         X = X/2
> >         self.dictionary['x'] = X
> >         Y = self.y1 + self.y2
> >         Y = Y/2
> >         self.dictionary['y'] = Y
> > c_x = -2
> > c_y = -2
> > d_x = 4
> > d_y = 3
> > midpoint_ = midpoint(c_x,d_x,c_y,d_y)
> > print midpoint_.dictionary
> >
> > It works and all, but I can't get the fraction or decimal from the
> > output :S
> >
> > Output:
> > {'y': 0, 'x': 1}
> >
> > On my paper the real output is:
> > x = 1
> > y = 1/2
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070828/34d8be93/attachment.html>


More information about the Python-list mailing list