An error or am I going mental


#1

In Carly’s clippers in python loops project.

The instructions in the list do not match the video , and the error is repeated again in the instructions at the end

I don’t know if I’m going crazy or not, those are much smarter people than I am so I’m struggling but it looks to me like they give the wrong range parameters.

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

prices and last_week and hair_styles are 8 long but the range is going to return [0,1,2,3,4,5,6]

If I’m correct than this is just ridiculous. The only way to check my work is to compare the results with the instructor in the video but it looks to me like he is 70 short in total revenue.

Or I’m missing something and I’m ranting for no reason lol. I’m very green to coding .


#2

Can you give your full code? We need to know the whole thing so we can help you.
Also use these symbols (```) to put your code. It keeps the forums tidy and clean :wink:


#3

That’s probably wrong yes. (And by probably I mean I don’t have testable code for your particular case with expected/actual result)

Generally you want to avoid indices altogether and instead say something like “iterate through all of these” because that’s not possible to get wrong. The zip function could help you iterate over multiple iterables at the same time, alternatively you’d group the data for each entry into one object so that it’s all in one place (which is exactly what zip would accomplish)

This is entirely correctly written by the way (think about it):

You can use range() in your list comprehension to make i go from 0 to len(new_prices) - 1 .


#4

Well this not about the code it’s about the video and instructions being misleading.
But this is my code which i think is correct. Check total revenue calculation to see the problem.

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

print("Average Haircut Price: " + str(average_price))

new_prices = [price -5 for price in prices]

print(new_prices)

total_revenue = 0

# in the video the instructor writes range(len(hairstyles)-1)
for i in range(len(hairstyles)):
  total_revenue += prices[i] * last_week[i]
  
print ("Total Revenue: " + str(total_revenue))

average_daily_revenue = total_revenue / 7

print(average_daily_revenue)

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

print(cuts_under_30)

test_array = [i for i in range(len(hairstyles))]
print(test_array)

test_array_minus_one = [i for i in range(len(hairstyles)-1)]
print(test_array_minus_one)

#5

*flicks through the video* yeah. video is wrong. instructions are correct.

On the other hand, it’s inconsistent with this one:

Use a for loop to create a variable i that goes from 0 to len(hairstyles)

Because they use the same wording but don’t subtract 1

Comes down to whether “to” is considered inclusive or not, it’s ambiguous


#6

So I’m mental then lol

the arrays we are iterating are 8 long

range(hairstyles) will return [0,1,2,3,4,5,6,7]

range(hairstyles)-1) will return [0,1,2,3,4,5,6] which is off by one as far as I understand

There is another len-1 at the last instruction

So to sum up it looks like the instructions are good in total revenue calculation but the video is not and in the last instruction they are both wrong ?


#7

Since they say it differently in two places, the instructions are wrong too. Individually they’re both correct because they’re ambiguous. The video is absolutely wrong, because it’s off by 1.

As programmers we need to reason about it in whatever format is comfortable, and then look up how various things actually behave, such as where range stops (or, again, better yet: use semantics that simply say “all of these”)


#8

Ugh, quite annoying. I hope the people making these things for novice programmers pay attention to what they’re doing, it’s just horrible when they don’t synchronize the code and what they’re saying


#9

This lesson issue has been referred upstairs a couple times in the past. That makes three exercises with this ambiguity.

The author seems to be reminding us that the range is one less than the length, and for their effort have gained lots of people’s pain. A problem easily solved once somebody gets around to it.