The problem is that the variable x is undefined when the just-in-time (JIT) compiler tries to make sense of the code. For your example it would be enough to define x later, but the JIT compiler cannot predict that.
You could for example initialise the variable in an outer scope, e.g. by adding x = missing or x = 0 before the for loop.
(A small side note: range(1,10) is the same as 1:9, not 1:10)