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