Multiplication error in python

Tim Daneliuk tundra at tundraware.com
Tue Sep 27 21:12:33 CEST 2011


On 9/27/2011 12:50 PM, sakthi said this:
> On Sep 27, 1:43 pm, Chris Rebert <c... at rebertia.com> wrote:
>> On Tue, Sep 27, 2011 at 10:21 AM, sakthi <sakth... at gmail.com> wrote:
>>> In the following code,
>>>>>> l=[1,2,3,4,5]
>>>>>> i=0
>>>>>> for a in l:
>>> ...     p=2*a
>>> ...     t=p+i
>>> ...     i=t
>>> ...
>>>>>> t
>>> 45
>>
>>> Python gives an answer as 45. But i am getting 30 when i execute
>>> manually. Is there any different multiplication pattern in python?
>>> Thank yu.
>>
>> My Python gives 30; methinks perhaps you've elided some important part
>> of your code.
>> Also, I note that your loop body can be significantly simplified to
>> just: i += 2*a
>>
>> Cheers,
>> Chris
>> --http://rebertia.com
> 
> if i put i += 2*a it returns 155.

Are you sure you don't have something indented incorrectly?

How about a plain text copy of your program (without the interactive
stuff) so we can run it independently...


-- 
------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com



More information about the Python-list mailing list