I am reading Python Programming by John Zelle and I am stuck on one the exercises shown in the picture below.

You can view my code below. I know the code is very ugly. (Any tips are appreciated)

![Picture of Regression Exercise](https://i.stack.imgur.com/bskw3.jpg)

Here's my code so far:

```
from graphics import *

def regression():

# creating the window for the regression line
        win = GraphWin("Regression Line - Start Clicking!", 500, 500)
        win.setCoords(0.0, 0.0, 10.0, 10.0)

        rect = Rectangle(Point(0.5, 0.1), Point(2.5, 2.1))
        rect.setFill("red")
        rect.draw(win)
        Text(rect.getCenter(), "Done").draw(win)

        message = Text(Point(5, 0.5), "Click in this screen")
        message.draw(win)

        points = [] # list of points
        n = 0 # count variable
        sumX = 0
        sumY = 0

        while True:
                p = win.getMouse()
                p.draw(win)

# if user clicks in a red square it exits the loop and calculates the regression line
                if (p.getX() >= 0.5 and p.getX() <= 2.5) and (p.getY() >= 0.1 and p.getY() <= 2.1):
                        break

                n += 1 # count of the points

# get the sum of the X and Y points
                sumX = sumX + p.getX()
                sumY = sumY + p.getY()

# tuple of the X and Y points
                dot = (p.getX(), p.getY())
                points.append(dot)

        avgX = sumX / n
        avgY = sumY / n

        top = 0
        bottom = 0

# my ugly attempt at the regression equation shown in the book

        for i in points:
                gp = 0
                numer = points[gp][0] * points[gp][1]
                top = top + numer

                denom = points[gp][0] ** 2
                bottom = bottom + denom
                gp += 1

        m = (top - sumX * sumY) / (bottom - sumX ** 2)

        y1 = avgY + m * (0.0 - avgX)
        y2 = avgY + m * (10.0 - avgX)

        regressionline = Line(Point(0, y1), Point(10.0, y2))
        regressionline.draw(win)

        raw_input("Press <Enter> to quit.")
        win.close()

regression()

```

When I run the program the regression line never appears to be the real line of best fit. I believe I am interpreting the regression equation incorrectly in my code. What needs to be changed to get the correct regression line?