Q:

Updating variable in a function (python) not working?

I am still new in python and exciting about learning it. This time, I have a function in which I expect to update a variable and use it again without using for loops. Please look at my function below:
def x_func(y):
    x = 0.
    newx = 0.897*x + (y/3)
    x = newx
    return x

Example:
import numpy as np
y = np.array([1, 2, 3])

When I call my function, I get:
In [1]:x_func(y)
Out[1]: array([ 0.33333333,  0.66666667,  1.        ])

However, the expected result is:
Out[1]: array([ 0.33333333,  0.96563676,  1.8661761797])

I want to update x based on newx, and use it again in the function as the new input variable x.
Can somebody tell me what I am doing wrong?

A:

The result you're actually receiving is correct. Let's work through it here:
x = 0.
y = np.array([1, 2, 3])
newx = 0.897*x + (y/3)   # newx=([1/3, 2/3, 3/3])
x = newx                 # x = ([1/3, 2/3, 3/3])

...since ((0.897*0) + (y/3)) == (y/3), the returned values are as anticipated.
Why would you expect anything different?

