Carly's Clippers: 'bouffant' Is = $30, Not Less Than So...?

In the Carly’s Clippers Exercise, Step 12 and 13 (https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/projects/carlys-clippers), we are meant to " Use a list comprehension to create a list called cuts_under_30 that has the entry hairstyles[i] for each i for which new_prices[i] is less than 30 ." The data supplied is:

hairstyles = [“bouffant”, “pixie”, “dreadlocks”, “crew”, “bowl”, “bob”, “mohawk”, “flattop”]

prices = [30, 25, 40, 20, 20, 35, 50, 35]

The correct answer for Step 12 would be:

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

Printing cuts_under_30 produces: [‘bouffant’, ‘pixie’, ‘crew’, ‘bowl’]. I received these results, as did Matt in the accompanying video for the exercise.

Why is ‘bouffant’ included in this list? The price for that cut is not less than $30. It is equal to $30, a result that I would have expected had I used:

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

Since that appears as the solution in the video, why is it correct…?

Hello, @liquidray, and welcome to the forums!

I don’t see the prices list referred to in cuts_under_30.

Hint

Don’t forget what you did in step 5. :wink:

2 Likes

Right…

new_prices was established earlier as a list being (prices - 5). I was looking at prices and not new_prices, hence my confusion.

Loading detail_orientated_proofreading.exe into my brain right now. Thank you!

1 Like

When in doubt, you can always print. The first thing I do when I see something other than what I was expecting is print the values the code I have written is using. In this case:

print(new_prices)

added immediately prior to the list comprehension would have jogged your memory. Happy coding!

here the solution

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
total_revenue = 0

for price in prices:
  total_price += price
  print(total_price)
average_price = total_price/len(prices)
print()
print(average_price)
#list comprehension1
new_prices = [price - 5 for price in prices]
print()
print(new_prices)

for i in range(len(hairstyles)):
  total_revenue += prices[i]*last_week[i]
print()
print(total_revenue)
average_daily_revenue = total_revenue/7
print()
print(average_daily_revenue)
#list comprehension2
cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)-1) if new_prices[i] < 30]
print()
print(cuts_under_30)