Tiphe
Tiphe

Reputation: 3

It seems that my variable won't increment in my for

real beginner in python here D:

So, I have a list :

product_list = ['2.99', '3', 1, '0.99', '10', 0, '7.49', '1', 1]

And I want to do the sum of the numbers between ' ' and multiply these pairs of numbers at the same time, kind of like this :

total = total + 2.99*3
total = total + 0.99*10
total = total + 7.49*1

To do so I'm using a for :

for i in range(len(product_list)):
    total = total + float(product_list[i]) * float(product_list[i+1])
    print(total)
    i+=2

I try to increment i to skip the number of my list that isn't placed between ' '

This give me the following result:

8.97
11.97
12.96
22.86
22.86
22.86

While it should only give me this result:

8.97
18.87
26.36

So I'm kind of lost there.. to me it looks like my i is simply not incrementing, which would explain everything, but I can't seem to make it increment.. any idea?

Upvotes: 0

Views: 987

Answers (4)

AChampion
AChampion

Reputation: 30258

Changing i at the end of you loop has no effect because it is immediately assigned the next value in the range...
It looks like you are trying to iterate the list in 3s, so you can add a step to your range to step by 3, e.g.:

total = 0
for i in range(0, len(product_list), 3):
    total = total + float(product_list[i]) * float(product_list[i+1])
    print(total)

Output:

8.97
18.87
26.36

Assuming you can guarantee a multiple of 3 then you can do this with zip:

total = 0
i = iter(product_list)
for x,y,_ in zip(i, i, i):
    total += float(x)*float(y)
    print(total)

Would provide the same result.

Upvotes: 1

Rockybilly
Rockybilly

Reputation: 4510

In the loop you are constructing, you increment the value of i only in one iteration. When the current iteration ends, the i value is reset to the one in the range() function. (So you can only use the incremented value in the current iteration before it ends.) Given that you increment it just before iteration ends, it does nothing.

You can accomplish what you are trying to do with list parsing

for example:

range(len(product_list))[::2]

or the built-in range feature;

range(0, len(product_list), 2)

What these basically does is that they create step values for the lists. So they generate this;

[1, 3, 5, 7] # etc...

Upvotes: 0

Amit Gold
Amit Gold

Reputation: 767

When you use for .. in, the value of the variable is reset every time. Meaning:

for i in range(3):
    print(i)
    i *= 2  # Any edit to i will behave the same

Will print the following:

0
1
2

because the value of i is independent of the previous loop.

That is different from loops in C(or similar languages) where you use for(int i = 0; i < 3; i++, the value of i is increased by 1, meaning it is dependent on the previous.

Please comment if the explanation isn't clear enough.

Upvotes: 0

greenwolf
greenwolf

Reputation: 214

for i in range(x) means you're iterating over a list [1,2 .. x], so when you change i inside the loop, it is changed only for current iteration, not the next.

You can use for i in range(0, len(product_list), 3), where 3d argument is step.

Upvotes: 0

Related Questions