TypeError in Code

In my Python excercise below, I am getting the following error when this line is processed:

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

File “script.py”, line 28, in
cuts_under_30 = [prices[i] for i in range(prices) if new_prices[i] < 30]
TypeError: ‘list’ object cannot be interpreted as an integer

Not sure what is going…Thanks, Alex

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 += price
average_price = total_price/len(prices)

print(“Avergge Haircut Price:” + " " + str(average_price))

new_prices = [price - 5 for price in prices]

print(new_prices)

total_revenue = 0

for i in range(len(hairstyles)):
total_revenue += prices[i] * last_week[i]
print(“Total Revenue:{}”.format(total_revenue))

average_daily_revenue = total_revenue/7
print(average_daily_revenue)

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

print(cuts_under_30)

range expects an integer. prices is a dictionary.

1 Like

Is this a list as opposed to a dictionary>…I am wondering if I need to convert the list to an integer in the list comprehension statement?

Both new_prices and cuts_under_30 are list objects.

The input list is integers, but the output list will be strings.

Note that we start with two lists, prices and hairstyles. The latter is made up of strings, the former, of integers.

We create the new_prices list by iterating over prices and subtracting 5 from each one and appending it to the new list.

Next we iterate over new_prices and obtain the index of every price less than 30. The corresponding style is then appended to the output list.

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