Carly’s Clippers project - module 3 problem with range (step 8)

Hello I struggle to understand why I get different total revenue (1085 instead of 1015) in the range part!

my code:

for i in range(len(hairstyles)):
total_revenue += (prices [i] * last_week[i])
print (total_revenue)

what the video shows:

for i in range(0, len (hairstyles) - 1)):
total_revenue += (prices [i] * last_week[i])
print (total_revenue)

Why is my code wrong? The number of hairstyles is 8, it should cover 0,1,2,3,4,5,6,7 = all 8 elements…I thought he didn’t explain his reasoning for the -1 very well but it appears to work.

Any help is appreciated thanks :).

Hi @khitan,

The video is in error. It should be …

for i in range(len(hairstyles)):

In a range, the stop value is exclusive, meaning it is not included in the result. Since len(hairstyles) is 8, the final value of i in the iteration is 7, which is the index of the last item in each of the lists.

If you subtract 1, the final items in the lists will be missed.

This output is correct …

Total Revenue: $1085.00

Thanks for the swift reply! Greatly appreciate it!

1 Like

Hello there,
i am stuck on the same step. For a reason I can’t quite udnerstand, I get an error message when I write my Code like this:
Error:
Traceback (most recent call last):
File “script.py”, line 20, in
total_revenue += prices[i] * last_week[i]
TypeError: ‘int’ object is not iterable

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 = sum(prices)
print(total_price)

average_price = total_price / len(prices)
print("Average Haircut Price: " + str(average_price))

new_prices=[]
for price in prices:
  new_prices.append(price - 5.0)

total_revenue = []

for i in range(len(hairstyles)):
  total_revenue += prices[i] * last_week[i]

print(total_revenue)

Where is the difference between the correct way of writing this Code and my solution?
Thanks in Advance!

It’s always worth breaking this sort of thing down and carefully checking the error message. An error on line 20 suggests there’s an error on this line or the lines immediately preceding it.

If you broke this down into separate steps it would be worth checking does the loop iterate through the values you want? Does the indexing perform what you expect for prices and last_week?

What about total_revenue? Can you add values to this using the += allow you to add a single integer? No? In the case of lists += behaves likes the list.extend() method. Why might this be a problem? How is this different to the working version?