Help me with this code

C

chovdary

Hi friends

help me with the following code. Im able to execute the code but getting wrong output

def sequence_b(N):
N = 10
result = 0
for k in xrange (1,N):
result += ((-1) ** (k+1))/2*k-1
print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19
 
M

MRAB

Hi friends

help me with the following code. Im able to execute the code but getting wrong output

def sequence_b(N):
N = 10
result = 0
for k in xrange (1,N):
result += ((-1) ** (k+1))/2*k-1
print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19
1. Multiplication and division take precedence over addition and
subtraction, and both multiplication/division and addition/subtraction
are calculated left-to-right, so an expression like x/2*k-1 is
calculated as ((x/2)*k)-1.

2. In Python 2, dividing an integer by an integer gives an integer
result, e.g. 7/2 gives 3, not 3.5. The simplest way of fixing
that is to introduce a float into either the numerator or denominator.

That means that your expression should be, say:

((-1) ** (k + 1)) / (2.0 * k - 1)

3. It won't print fractions like 1/5, but instead decimals like 0.2.

4. You're adding the result of the expression to 'result' and then
printing the value of 'result', so your output would be the results of:

-1
-1+(-1/3)
-1+(-1/3)+(1/5)

and so on.

5. Your function prints the result but doesn't return anyhing, so, by
default, it'll return None. Therefore, 'print sequence_b(10)' will
print 'None'. What you'll get is a series of numbers and then a None.
 
S

Steven D'Aprano

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)
 
D

Dave Angel

result += ((-1) ** (k+1))/2*k-1

One of two things are happening here. Maybe both.

You're using Python 2.x (and should havesaid so) where integer
division is truncated.

You're missing around some part of the intended denominator. Check
operator precedence rules.
 
P

Piet van Oostrum

Hi friends

help me with the following code. Im able to execute the code but getting wrong output

def sequence_b(N):
N = 10
result = 0
for k in xrange (1,N):
result += ((-1) ** (k+1))/2*k-1
print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19

You probably want this:

N = 10
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
result += step
print(step)

Note:
1. You don't use the parameter N, you immediately change it to 10. Leave the line N = 10 out.
2. Your function doesn't return its result, so it returns None. So the print sequence_b(10) dosn't make sense.
If the print is only for debugging the use the following:

def sequence_b(N):
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
print(step) ## debug output
result += step
return result

print(sequence_b(10)) # print the result of the function call

[I use print() because I use Python 3]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top