Array Help - Rainfall Interpolation

Viewed 34

I am writing an array program for class and can't figure out the error. The program actually runs with out any coding errors but doesn't prompt for input like it should. PyCharm is telling me that in the "slope = (y[i2] - y[i1] / (x[i2] - x[i1])" line that the i2 and i1 values for y "Local variable 'i2' might be referenced before assignment'. I'm not sure what that means.

def interpolate(x, y, time):
    i = 0
    for value in x:
        i = i + 1
        if value >= time:
            i1 = i - 2
            i2 = i - 1
            break
    slope = (y[i2] - y[i1]) / (x[i2] - x[i1])
    res = y[i1] + (slope * (time - x[i1]))
    return res


def main():
    x = [0, 1, 2, 5, 7, 8, 10, 12, 14]
    y = [0, .4, .6, 1.3, 2.1, 2.9, 3.4, 3.7, 3.9]
    time = float(input("Enter time between 0 and 15: "))
    res = interpolate(x, y, time)
    print(res)
    main()
0 Answers
Related