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


#1

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


#2

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

#3

Thanks for the swift reply! Greatly appreciate it!