Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58545
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.018 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'output': 0.05; 'float': 0.07; 'result,': 0.07; 'subject:code': 0.07; '-10': 0.09; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; '-18': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'none.': 0.16; 'precedence': 0.16; 'simplest': 0.16; 'xrange': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'print': 0.22; 'header:User- Agent:1': 0.23; 'integer': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'gives': 0.31; 'code': 0.31; 'getting': 0.31; 'calculated': 0.31; 'default,': 0.31; 'division': 0.31; 'fixing': 0.31; 'prints': 0.31; 'subject:with': 0.35; 'but': 0.35; 'should': 0.36; 'wrong': 0.37; 'so,': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'expression': 0.60; 'numbers': 0.61; "you're": 0.61; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'therefore,': 0.64; 'series': 0.66; 'header :Reply-To:1': 0.67; 'of:': 0.68; 'results': 0.69; 'reply-to:no real name:2**0': 0.71; 'introduce': 0.78; 'friends': 0.81; 'subject:this': 0.83; "'result'": 0.84; 'reply- to:addr:python.org': 0.84 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=J66TsmXS c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=2LCq-pQfA8cA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=LWINwjFLEAcA:10 a=pGLkceISAAAA:8 a=a737Z_RHk1px362-2vEA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Wed, 06 Nov 2013 02:37:34 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Help me with this code |
| References | <612c0028-ae66-4200-b9a5-a613eca94762@googlegroups.com> |
| In-Reply-To | <612c0028-ae66-4200-b9a5-a613eca94762@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2072.1383705462.18130.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1383705462 news.xs4all.nl 15927 [2001:888:2000:d::a6]:40065 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:58545 |
Show key headers only | View raw
On 06/11/2013 01:51, chovdary@gmail.com wrote:
> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next 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