Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Python 2.6 code behaves differently inside function than outside it
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 03-21-2011, 09:58 AM #1 dracuss Member   Registered: May 2006 Location: Chisinau, Moldova Distribution: Gentoo, Debian sid Posts: 151 Rep: 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] >>> b=[10,19,0] >>> x_in=[0,0,0] >>> def jacobi(a,b,x_in): ... if len(a)==len(b)==len(x_in): ... n=len(a) ... x_fin=x_in ... print("N={0}".format(n)) ... for i in range(n): ... print ("Starting operation for row {0}".format(i)) ... temp=b[i] ... for o in range(i): ... temp=temp-x_in[o]*a[i,o] ... for o in range(i,n): ... temp=temp-x_in[o]*a[i,o] ... x_fin[i]=temp/float(a[i,i]) ... return x_fin ... >>> jacobi(a,b,x_in) N=3 Starting operation for row 0 [1.1111111111111112, 0, 0] >>> jacobi(a,b,x_in) N=3 Starting operation for row 0 [0.0, 0, 0]``` While, when I write the same code, but outside the function, I get the normal answers for the first iteration: Code: ```>>> for i in range(n): ... print ("Starting Operation for row {0}".format(i)) ... temp=b[i] ... for o in range(i): ... temp=temp-x_in[o]*a[i,o] ... for o in range(i,n): ... temp=temp-x_in[o]*a[i,o] ... x_fin=temp/float(a[i,i]) ... print x_fin ... Starting Operation for row 0 1.11111111111 Starting Operation for row 1 1.9 Starting Operation for row 2 0.0``` What did I do wrong? It looks like I'm missing something. Thanks for your attention.
 03-21-2011, 11:35 AM #2 Snark1994 Senior Member   Registered: Sep 2010 Location: Wales, UK Distribution: Arch Posts: 1,632 Blog Entries: 3 Rep: 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 ... for i in range(n): ... print ("Starting operation for row {0}".format(i)) ... temp=b[i] ... for o in range(i): ... temp=temp-x_in[o]*a[i,o] ... for o in range(i,n): ... temp=temp-x_in[o]*a[i,o] ... x_fin[i]=temp/float(a[i,i]) ... return x_fin``` with Code: ```>>> #Your loop code (indented to match the previous code) >>> for i in range(n): ... print ("Starting Operation for row {0}".format(i)) ... temp=b[i] ... for o in range(i): ... temp=temp-x_in[o]*a[i,o] ... for o in range(i,n): ... temp=temp-x_in[o]*a[i,o] ... x_fin=temp/float(a[i,i]) ... print x_fin ...``` The 2nd 'for o in range' in the first codeblock seems to be at the wrong indentation - by my reckoning it should currently raise a syntax error due to unexpected indentation. 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, Last edited by Snark1994; 03-21-2011 at 11:42 AM.
 03-21-2011, 12:51 PM #3 dracuss Member   Registered: May 2006 Location: Chisinau, Moldova Distribution: Gentoo, Debian sid Posts: 151 Original Poster Rep: It was a problem with gvim Thank you very much for your help

 Tags error, function, python

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post peridian Linux - Security 1 02-20-2011 01:31 PM jjge Linux - Software 1 03-16-2010 08:55 AM alred Programming 2 07-20-2006 02:21 AM naijaguy Programming 3 01-07-2005 02:09 PM Warmduvet Linux - General 22 09-22-2004 05:05 PM

LinuxQuestions.org

All times are GMT -5. The time now is 12:04 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -