Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58648
| From | Piet van Oostrum <piet@vanoostrum.org> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Help me with this code |
| Date | 2013-11-07 09:08 -0400 |
| Message-ID | <m2y550z7ex.fsf@cochabamba.vanoostrum.org> (permalink) |
| References | <612c0028-ae66-4200-b9a5-a613eca94762@googlegroups.com> |
chovdary@gmail.com writes:
> 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]
--
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Help me with this code chovdary@gmail.com - 2013-11-05 17:51 -0800 Representing fractions (was: Help me with this code) Ben Finney <ben+python@benfinney.id.au> - 2013-11-06 13:06 +1100 Re: Help me with this code MRAB <python@mrabarnett.plus.com> - 2013-11-06 02:37 +0000 Re: Help me with this code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-06 02:42 +0000 Re: Help me with this code Dave Angel <davea@davea.name> - 2013-11-05 23:44 -0600 Re: Help me with this code Piet van Oostrum <piet@vanoostrum.org> - 2013-11-07 09:08 -0400
csiph-web