Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
bartleby

Videos

Question
Book Icon
Chapter 8, Problem 13PE
Program Plan Intro

Regression Line

Program plan:

  • Import the header file.
  • Define the “graphWin” method
    • Create a window
    • Set the coords
    • Display the message on the window
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method for set the point
    • Call the “Line” method for draw the line
    • Call the “draw” method
    • Call the “Rectangle” method
    • Get the center position
    • Set the “Done” text
    • Call the “draw” method stop the program after clicking the “Done” text
    • Accept the input from the user
    • Set the list
    • Make the condition true
      • Get the mouse action
      • Check the condition
        • Break the loop
      • Otherwise, store user points in an appended list
      • Call the “draw” method
    • Return the result
  • Define the “average” method
    • Set the values for the variables
    • Iterate “i” until it reaches “allPoints” value
      • Get the “x” and “y” value
      • Calculate “sumX”, and “sumY”
      • Increment the “count” value
      • Calculate the “sumXiYi, “sqX”, “sumSqXi”, “SqY”, and sumSqYi” values
    • Calculate the “a” and “b” values
    • Return the values
  • Define “main” method
    • Display the text on the window
    • Call the “average” method
    • Set the values
    • Call the “Line” method
    • Call the “draw” method
    • Get the mouse action
  • Call the function “main()”.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

#import the required header files

from graphics import *

import math as m

#definition of "graphWin" method

def graphWin(Title):

    #Create a graphics window

    win = GraphWin(Title, 400, 400)

    #set the coords

    win.setCoords(-10, -10, 10, 10)

    #tell user to select multiple locations on the screen to designate points

    message = Text(Point(-3, 8), "Click to delineate points on the graph.")

    #draw the point

    message.draw(win)

    #call the "Line" method

    axisX = Line(Point(-10,0), Point(10,0))

    #call the "draw" method

    axisX.draw(win)

    #call the "Line" method

    axisY = Line(Point(0,10), Point(0,-10))

    #call the "draw" method

    axisY.draw(win)

    #call the "Rectangle" method

    r = Rectangle(Point(-9, -9), Point(-7,-8))

    #call the "draw" method

    r.draw(win)

    #get the centre position

    rCenter = r.getCenter()

    #set the text

    stopMouse = Text(rCenter, "Done")

    #call the "draw" method

    stopMouse.draw(win)

    #accept input from user until <done> button is pressed

    click = Point(0,0)

    #set the list

    allPoints = []

    #check the condition

    while True:

        #get the mouse action

        click = win.getMouse()

        #check the condition

        if ((-9 <= click.getX() <= -7) and (-9 <= click.getY() <= -8)):

            #break the loop

            break

        #otherwise

        else:

            #store user points in an appended list

            allPoints.append(click)

            #call the "draw" method

            click.draw(win)

    #return the values

    return allPoints, win

#definition of "average" method

def average(allPoints):

    #set the values for the variables

    sumX = 0

    sumY = 0

    count = 0

    sumXiYi = 0

    sumSqXi = 0

    sumSqYi = 0

    #iterate "i" until it reaches "allPoints" value

    for i in allPoints:

        #get the "x" and "y" value

        x = i.getX()

        y = i.getY()

        #calculate the "sumX" value

        sumX = sumX + x

        #calculate the "sumY" value

        sumY = sumY + y

        #increment the value

        count = count + 1

        #calculate the "xy" value

        xy = x * y

        #calculate the "sumXiYi" value

        sumXiYi = sumXiYi + xy

        #calculate the "SqX" value

        SqX = x * x

        #calculate the "sumSqXi" value

        sumSqXi = sumSqXi + SqX

        #calculate the "SqY" value

        SqY = y * y

        #calculate the "sumSqYi" value

        sumSqYi = sumSqYi + SqY

    #calculate the "a" and "b" value

    a = ((sumY * sumSqXi) - (sumX * sumXiYi)) / (count * (sumSqXi) - sumX ** 2)

    b = ((count * sumXiYi) - (sumX * sumY)) / (count * (sumSqXi) - sumX ** 2)

    #return the values

    return a, b

#definition of "main" method

def main():

    #get the text for the window

    allPoints, win = graphWin("Regression Line")

    #call the method

    a, b = average(allPoints)

    #set the values

    x1 = -10

    x2 = 10

    #call the "Line" method

    regressLine = Line(Point(x1, (a + b * x1)), Point(x2, (a + b * x2)))

    #call the "draw" method

    regressLine.draw(win)

    #get the mouse action

    win.getMouse()

#call the "main" method

main()

Sample Output

Output:

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  1

After clicking “Done” button, the below window will appear

Screenshot of “Regression Line” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 8, Problem 13PE , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
EX:[AE00]=fa50h number of ones =1111 1010 0101 0000 Physical address=4AE00h=4000h*10h+AE00h Mov ax,4000 Mov ds,ax; DS=4000h mov ds,4000 X Mov ax,[AE00] ; ax=[ae00]=FA50h Mov cx,10; 16 bit in decimal Mov bl,0 *: Ror ax,1 Jnc ** Inc bl **:Dec cx Jnz * ;LSB⇒CF Cf=1 ; it jump when CF=0, will not jump when CF=1 HW1: rewrite the above example use another way
EX2: Write a piece of assembly code that can count the number of ones in word stored at 4AE00h
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask a question, then display one of the responses, randomly selected from the list. The program should repeat until the user is ready to quit. Contents of 8_ball_responses.txt:  Yes, of course! Without a doubt, yes. You can count on it. For sure! Ask me later. I'm not sure. I can't tell you right now. I'll tell you after my nap. No way! I don't think so. Without a doubt, no. The answer is clearly NO. (You can access the Computer Science Portal at www.pearsonhighered.com/gaddis.)
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY