Alan Gauld alan.gauld at btinternet.com
Fri Oct 15 08:33:46 CEST 2010

```"Colleen Glaeser" <songbird42371 at gmail.com> wrote

> I am in a beginning-level computer science class in college
> and am running into problems with an assignment.

Because its homework we can't give you a fiull solution
but  can give hints so...

>   The regression line is then given by
>
> where m and b may be obtained by

Thre appears to be something missing?

> So far, my program is as follows:
>
> Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]]
>
> def X():
>     accX = 0
>     for item in Data:
>        [x,y] = item
>
>        accX = accX + x
>    print (accX)

You don't take any data in or give any data out so the results
are stored in local variables (x,y, accX) which are thrown away
when the function exits. It also only ever works on the global value
Data.
It would be better to pass Data in as a parameter and to return
the results so you can use them outside the function.

Also X() is not a very descriptive name for your function. It will be
much easier to figute out what your program does if you use
descriptive names. A good name for this one might be accX?

> def Y():

All of the above applies here too.
Also this is so similar to the previous function that might it be
possible to do it with one function that takes a parameter to
determine which coordinate to sum? It couldbe caloled sumCoord()
and the def would look like:

def sumCoord(data, coord):

> def P():
> def Q():

And again for these functions.

> def B():
>     ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2))

Because your functions don;t return any values the multiplications
have no values to multiply.

As a general rule its best to keep print statements out of functions
(except for debugging) and print the results instead.

Thus you could do

print X()
print B()

etc.

> functions for B and M are not working.  I'm not sure if there is a
> way to
> make functions into variables or how to solve this problem.

You need to get your functions to return their results so that you can
store them in variables(or use them directly in your calculation)

> Second, I am confused as to what my teacher means to do
> when it comes to inputting different values of x.

Another problem for another email :-)

Take a look at the functions and modules topic of my tutor for
a reminder of how to write function.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

```