Dec-10-2025, 09:15 PM
(This post was last modified: Dec-12-2025, 09:13 PM by deanhystad.)
Im trying to make a program that will compute a regression line and then display it. First I click on the window to get the points and then when I click on the rectangle that says 'done' the loop will stop and then display the line of best fit. However the loop only goes through one iteration before stopping and giving me a division by zero error(m=numerator/denominator) . Sorry if its not formatted right I asked ai to do it for me
def main():
print("Click anywhere in graph window ten times to get points")
x = 0
y = 0
click_count = 0
avgOfX = 0
avgOfY = 0
productOfXandY = 0
productOfX = 0
m = 0
numerator = 0
denominator = 0
y_eqOne = 0
y_eqTwo = 0
win = GraphWin("regression line", 400, 400)
rect = Rectangle(Point(3, 3), Point(60, 65))
rect.draw(win)
label = Text(Point(30, 30), "Done")
label.draw(win)
while i <= 10:
click_point = win.getMouse()
click_count += 1
i = i + 1
mouse = click_point
mouse.draw(win)
click_x = mouse.getX()
click_y = mouse.getY()
p1 = rect.getP1()
p2 = rect.getP2()
rect_x1 = p1.getX()
rect_y1 = p1.getY()
rect_x2 = p2.getX()
rect_y2 = p2.getY()
x = x + click_x
y = y + click_y
# Stop if "Done" rectangle is clicked
if (rect_x1 <= click_x <= rect_x2) and (rect_y1 <= click_y <= rect_y2):
break
avgOFx = x / click_count
avgOFy = y / click_count
productOFandY = click_x * click_y
productOFx = click_x * click_x
sumOFx = sumOFx + productOFx
sumOFxandY = sumOFxandY + productOFandY
numerator = sumOFxandY - click_count * (avgOFx * avgOFy)
denominator = sumOFx - click_count * (avgOFx ** 2)
m = numerator / denominator
y_engOne = y - avgOFy + m * (x - avgOFx)
y_eqOne = y_engOne
y_eqTwo = y_eqOne
ln = Line(Point(10, y_eqOne), Point(100, y_eqTwo))
ln.draw(win)
Gribouillis write Dec-11-2025, 03:34 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
