Python 2.6 code behaves differently inside function than outside it
Hello,
I'm trying to write a small function that computes the linear equation systems by using the Jacobi iterative method. But I'm having such a problem: There is a piece of code, that inside a function computes only for the first iteration and then it stops, giving wrong answer. Here is the code, right from the Python shell: Code:
>>> a=[[9,1,1],[2,10,3],[3,4,11] Code:
>>> for i in range(n): |
The first time you typed it, you've got dodgy indentation which means it doesn't work the same as the second time...
Compare Code:
... #Second half of the function Code:
>>> #Your loop code (indented to match the previous code) Also, the 'x_fin=temp/float(a[i,i])' is at different indentations in the two code blocks. I don't actually know the algorithm you're describing, so I'm not sure which is correct, but one of them must be wrong ;) Finally, I think the issue which is causing you grief is the fact that you probably mean to move the 'return' to the same indentation level as the 'for i in range(n):' The first one is going to work through the two loops and then return to the shell. The second one, however, prints the array out and continues through the 'for i in range(n):' loop :) you probably meant to put the return statement after the 'for i in range(n)' loop Hope this helps, |
It was a problem with gvim :) Thank you very much for your help
|
All times are GMT -5. The time now is 10:49 PM. |