On Tue, 05 Nov 2013 17:51:00 -0800, chovdary wrote: > Hi friends > > help me with the following code. Im able to execute the code but getting > wrong output [snip code]
You have this critical expression in your code: result += ((-1) ** (k+1))/2*k-1 It looks to me like you are using Python 2. Unfortunately, Python 2 had a design flaw that wasn't corrected until Python 3, where division is the integer division operator: 1/2 => 0 rather than 0.5. There are three ways to fix this: 1) convert one of the numbers to a float: result += float((-1) ** (k+1))/2*k-1 will probably do the trick. Although I'm not sure if the denominator is meant to be just 2, as you have above, or (2*k-1). 2) Put "from __future__ import division" as the very first line of your program. It must appear before any other code. 3) Avoid floating point numbers and use actual fractions, which will give you exact values instead of approximate. At the beginning of your code, put from fractions import Fraction and then change the code to look something like this: result += Fraction((-1) ** (k+1))/Fraction(2*k-1) -- Steven -- https://mail.python.org/mailman/listinfo/python-list