Multiplication error in python

becky_lewis bex.lewis at gmail.com
Tue Sep 27 14:51:06 EDT 2011


I think you may be doing something wrong.

If I run:
>>> l = [1, 2, 3, 4, 5]
>>> i = 0
>>> for a in l:
    ...    i += 2 * a
    ...
>>> i
 30

The result is 30 as expected.

Nowhere near the 155 that you are getting. :/

Again, could you state which version of python are you using (and what
OS) so that we can run the code in the same environment as you.

Becky Lewis


>
> > > 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.




More information about the Python-list mailing list