Multiplication error in python

G

Gary Herron

In the following code,
l=[1,2,3,4,5]
i=0
for a in l:
... p=2*a
... t=p+i
... i=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.

I think it's most likely you've made an error here. Running your exact
lines in Python gives the expected result:
... p=2*a
... t=p+i
... i=t
...30

Please try again. If you can reproduce the incorrect result -- then we
have a real bug. However, the chance of that is really really tiny.

Gary Herron
 
B

becky_lewis

I tried running your code in ipython:

In [1]: l = [1,2,3,4,5]
In [2]: i = 0
In [3]: for a in l:
...: p = 2 * a
...: t = p + i
...: i = t
...:
In [4]:
In [4]: t
Out[4]: 30

The output from Python was 30, not 45. What Python version are you
running?

In the following code,>>> l=[1,2,3,4,5]
...     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.
 
C

Chris Rebert

In the following code,
l=[1,2,3,4,5]
i=0
for a in l:
...     p=2*a
...     t=p+i
...     i=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
 
S

sakthi

In the following code,
l=[1,2,3,4,5]
i=0
for a in l:
...     p=2*a
...     t=p+i
...     i=t
...

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

becky_lewis

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

Tim Daneliuk

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

Chris Angelico

My guess is that you actually typed
   p=3*a
instead of
   p=2*a

That produces 45.

Or alternatively, that you used an interactive Python environment and
didn't clear i between runs.

ChrisA
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top