Need help with the Carly's Clippers project



I’m struggling to get this project going, currently stuck at the point 8:

  • Use a for loop to create a variable i that goes from 0 to len(hairstyles)-1 (you can use range() to do this!)

English being not my main language, I’m not sure how to proceed. I already defined the total_revenue = 0 variable the prior step required but I don’t know how to continue


How would one construct a loop over a list’s indexes, given length? The hint above suggests using range(). Try a couple practice loops in the console using print to see instant results.


I wrote this

for i in hairstyles:
i = range(0, (len(hairstyles) -1))

And this is what I get

range(0, 7)
range(0, 7)
range(0, 7)
range(0, 7)
range(0, 7)
range(0, 7)
range(0, 7)
range(0, 7)

My issue is that I cannot seem to understand what the excersise is asking me to do, in logical terms

Next step indicates:

Add the product of prices[i] (the price of the haircut at position i ) and last_week[i] (the number of people who got the haircut at position i ) to total_revenue at each step.

This one is even more confusing


Start with a range.

for i in range(len(hairstyles)):

Recall that the last (highest) value is not included in the range. This is useful when it comes to lists. range defaults to zero as the starting value if not specified.

Within that loop you will access (look up) index i in two separate lists. Their product will go into the prices list, at index i.

list1[i] * list2[i]


Well, this is helpful but still I cannot get my head around it, this is the first time i get mindblocked so hard

total_revenue = 0

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

and I get this error

TypeError: list indices must be integers or slices, not tuple

I’m going to go over the whole loops chapter seems there is something I miss


We cannot give a float or an integer to the list constructor.

>>> list(1)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> list(3.14)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
TypeError: 'float' object is not iterable
prices[i] * last_week[i]

Recall that we are accumulating a total, not assigning it anew. That will give only the final value assigned.

a += b * c

Sorry if this created confusion. Should have stuck to letters.


After a while I got around it. Thank you! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.