1

I am writing a program that operates out of a main() function. I am unable to change the main function (this is part of a class). How would I go about carrying over a variable from one function to the next, without changing the main()?

def read_data(fname) :
    file = fname
    x = []
    y = []
    with open(file) as f:
        for line in f:
            xi, yi = [float(x) for x in line.split(",")]
            x.append(xi)
            y.append(yi)
    return x, y   

def compute_m_and_b(x, y) :     
    sx, sy, sx2, sxy, sy2 = 0, 0, 0, 0, 0
    for i in range(len(x)):
        sx += x[i]
        sy += y[i]
        sx2 += (x[i] ** 2)
        sy2 += (y[i] ** 2)
        sxy += (x[i] * y[i])       
    m = (sxy * len(x) - sx * sy) / (sx2 * len(x) - sx**2)
    b = (sy - m * sx) / len(x)
    return m, b

def compute_fx_residual(x, y, m, b) :
    fx = []
    for xi in x:
        fx.append(m * xi + b)    
    residual = []
    for i in range(len(y)):
        residual.append(y[i] - fx[i])
    return fx, residual
        
def compute_sum_of_squared_residuals(residual) :
    least_squares_r = 0
    for i in range(len(y)) :
        least_squares_r += (residual[i]) ** 2
    return least_squares_r

def compute_total_sum_of_squares(y) :
    sum_squares = 0
    ymean = sum(y) / len(y)
    for i in range(len(y)) :
        sum_squares += (yi - ymean) ** 2
    return sum_squares

as you can see, I am restricted to only pulling the variables listed in the parentheses of the def functions. This leads to variables calculated in prior functions being undefined. How can I import them without needing to change the main()?

Please let me know if I should be more clear. I can provide more examples, but I wanted to maintain some brevity.

EDIT: here is the main function:

    def main():
        fname = input("Enter Input Filename: ")
    
        x, y = regress.read_data(fname)
        print()
        print("Input File: ", fname)
        print("Data points: ", len(x))
        
        #compute coefficients m and b
        m, b = regress.compute_m_and_b(x, y)
        
        #compute fx and residual
        fx, residual = regress.compute_fx_residual(x, y, m, b)
        
        #compute sum of squared residuals
        least_squares_r = regress.compute_sum_of_squared_residuals(residual)
        
        #compute sum of squares
        sum_squares = regress.compute_total_sum_of_squares(y)
        
        #compute coefficeint of determination
        coeff_of_d = regress.compute_coeff_of_determination(least_squares_r, sum_squares)
        
        regress.print_least_squares(x, y, m, b, fx, residual, least_squares_r, sum_squares, coeff_of_d)
        
        #compute pearson coefficient
        pearson_r = regress.compute_pearson_coefficient(x, y)
    
        regress.print_pearson(pearson_r)
    
    
        return
    
        
    main()
smallvt
  • 63
  • 6
  • Where is the main function? what vars do you want to import where? – Chris Jun 28 '21 at 01:07
  • 1
    If you want your variables to be carried over from one function to another, you could assign them as ```global``` variables by doing the following under the function you want the variables to be passed into: ```global x, y, etc``` – Omar AlSuwaidi Jun 28 '21 at 01:50
  • "class" as in classroom or in object-oriented programming? Also, please show your `main` function if possible. – Code Different Jun 28 '21 at 01:57
  • 1
    @OmarAlSuwaidi one should avoid using `global` where at all possible. – blueteeth Jun 28 '21 at 10:00
  • @CodeDifferent Class as in classroom (asynchronous learning, Teacher's hours line up poorly with mine. And the main goes through computing each variable-- ill edit the post – smallvt Jun 28 '21 at 20:23
  • @ChristopherHolder I will edit the post to include main – smallvt Jun 28 '21 at 20:23

1 Answers1

2

You haven't provided the main function so it's unclear how you're currently using it.

Looks to me like you can just get the variable for each consecutive function and pass them into the next:

fname = "some/path/to/file.txt"

x, y = read_data(fname)
m, b = compute_m_and_b(x, y, m, b)
fx, residual = compute_fx_residual(x, y, m, b)
least_squares_r = compute_sum_of_squared_residuals(residual)
sum_squares = compute_total_sum_of_squares(y)
blueteeth
  • 3,330
  • 1
  • 13
  • 23
  • This makes sense, but the problem arises that I am unable to use (fname) and some others in the main function, which I have edited onto the post. I am not able to change main.py (it is a restraint of the work). Could I just add the variables to the return function perhaps? – smallvt Jun 28 '21 at 20:27
  • @smallvt I don't understand the problem. Which variables can't you access and where? Does this help: https://stackoverflow.com/a/292498/1904146 – blueteeth Jun 28 '21 at 22:46