# How to get a sum during a loop

Can someone tell me to get the sum of a loop? I can get the individual totals but not the grand total. Code below

hairstyles = [“bouffant”, “pixie”, “dreadlocks”, “crew”, “bowl”, “bob”, “mohawk”, “flattop”]

prices = [30, 25, 40, 20, 20, 35, 50, 35]

last_week = [2, 3, 5, 8, 4, 4, 6, 2]

total_price = 0

for price in prices:

total_price += price

average_price = total_price/len(prices)

print(“Average Haircut Price:”, average_price)

new_prices =

for new_price in prices:

new_prices = new_price - 5

print(new_prices)

total_revenue =

for i in range(len(hairstyles)):

total_revenue =+prices[i]*last_week[i]

print(“Total Revenue:”, total_revenue)

Hi ,

You almost have it, note that at one point you write `total_revenue =+prices[i]*last_week[i]` and that `=+` is not `+=`.

For future reference, please review this note on how to format code in posts as it makes it much easier to help with: [How to] Format code in posts

3 Likes

Thanks for the tip on adding code to the forum. Good catch on the += mistake. I did correct it but now I am getting Type Error :“Traceback (most recent call last):
File “script.py”, line 15, in
total_revenue +=prices[i]*last_week[i]
TypeError: ‘int’ object is not iterable”

Help!! Losing my mind here.

Hi,

The error messages in python are quite descriptive, so usually a readup helps clarify. In this case, `iterable` is referring to whether something can be cycled in a loop.

So integers are not supposed to be iterated.

``````>>> for i in 5:
...     i
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
``````
1 Like

I agree with you on error messages being quite descriptive (most of the time), but the line of code shown in my case does not match your example. Also, why just by changing =+ to += would show an error? When I had the incorrect =+ it would output the individual numbers ( but not sum them up) . I really appreciate your help on this, learned quite a lot.

Hi,

It’s hard to replicate the error without knowing your formatting. You can try editing the code and then we can see what’s happening

``````hairstyles = ["bouffant", "pixie", "dreadlocks", "crew", "bowl", "bob", "mohawk", "flattop"]
prices = [30, 25, 40, 20, 20, 35, 50, 35]
last_week = [2, 3, 5, 8, 4, 4, 6, 2]
total_price = 0
for price in prices:
total_price += price
average_price = total_price/len(prices)
print("Average Haircut Price:", average_price)
new_prices = []
for new_price in prices:
new_prices = new_price - 5
print(new_prices)
total_revenue = []
for i in range(len(hairstyles)):
total_revenue +=prices[i]*last_week[i]
print("Total Revenue:" +total_revenue)
``````

Careful with types, appending and doing a `+=` operation are slightly different.

1 Like

I finally got it!

Changed total_revenue = 0
and
print(“Total Revenue:” +str(total_revenue))

All thanks to you… Thank you so much

1 Like

didn’t get where I could tell the difference but thanks for pointing it out!

Consider this example:

``````>>> test = [1,2,3,4]
>>> test += 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> test.append(4)
>>> test
[1, 2, 3, 4, 4]
``````

and then

``````
>>> test += [5]
>>> test
[1, 2, 3, 4, 4, 5]
``````
2 Likes