Carly's Clippers - TypeError: 'int' object is not subscriptable

Hello,

I am stuck on the last part (12) of the Carly’s Clippers exercise. I watched the video associated and copied his code, but I am still getting an error.

Can someone please help me understand what is wrong and how I can fix it? I am stumped.

My full code below (please ignore the failed attempts I commented out):


[codebyte]
`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 = total_price + price
print(total_price)

num_of_prices = len(prices)

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

new_prices = prices

for price in new_prices:
  new_prices = price - 5
  
print(new_prices, end=" ")

  

total_revenue = 0

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



print("\nTotal Revenue: " + str(total_revenue))

average_daily_revenue = total_revenue / 7
print(average_daily_revenue)


# cuts_under_30 = " "


# for i in range(new_prices):
#   if new_prices[i] < 30:
#     cuts_under_30 += hairstyles[i]

# if new_prices[i] < 30:
#   while price in new_prices < 30:
#     cuts_under_30 += hairstyles[i] 


# thislist = ["apple", "banana", "cherry"]
# for i in range(len(thislist)):
#   print(thislist[i])

# list(new_prices[i])


cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]

print(cuts_under_30)
`

What I’m returning is:

255
Average Haircut Price: 31.875
30 
Total Revenue: 850
121.42857142857143
Traceback (most recent call last):
  File "script.py", line 58, in <module>
    cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
  File "script.py", line 58, in <listcomp>
    cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
TypeError: 'int' object is not subscriptable


Double check the first indicie. Should it be a 1?

Then, this part needs some tweaking:

for price in new_prices:
  new_prices = price - 5

Maybe create a variable, new_prices and set it equal to what you have, [price - 5 for.... (finish that bit)

Also, I’m not sure what’s going on with this print() statement:

print("\nTotal Revenue: " + str(total_revenue))

Is str() the right way to go about it?

Shouldn’t it be something like:

print('Total Revenue: ${}'.format(total_revenue))

It’s a good idea to comment out the parts and run the code to see exactly where the errors are. Then you can fix each part. Try it and see what works…

1 Like

thank you for your reply!

I will go back and try again with your feedback :slight_smile:

1 Like