Can't find and don't understand the mistake I have made in my code for the thread shed project.. 19-20 steps

Hello there, long story short, I’m just badly stuck (wanna rip my hair off) on the “thread shed” project, here in… I have tried to find an answer/explanation/fix across the forums, and there was even a thread with the same problem that I was having, but their ending was just to blame the system itself…So I really would appreciate all the help I could get…!

I’m stuck on the step 20 (Test your new function by running color_count('white') . It should return 28)
But it returns 112 for me, checking with other colors I have noticed that it ALWAYS returns a number that’s exactly 4 times bigger than the one that i was supposed to be getting (I mean, sure I could just divide by 4, but I kinda want to know where did I go wrong in the code itself x.x)

def color_count(color):
  ats = 0
  for i in thread_sold_split:
    if color == i:
      ats += 1
  return ats

daily_sales_replaced = daily_sales.replace(";,;", "___")

daily_transactions = daily_sales_replaced.split(',')

daily_transactions_split = []
transactions_clean = []

for i in daily_transactions:

for i in daily_transactions_split:
  transaction_clean = []
  for data_type in i:
thread_sold_split = []
customers = []
sales = []
thread_sold = []
total_sales = 0

for i in transactions_clean:

for i in sales:
  total_sales += float(i.strip("$"))

for i in thread_sold:
  for letters in i:
    splat = []
    if letters == "&":
      splat = i.split("&")
      for u in splat:
  if splat == []:


Edit: Found the mistake finally… “transactions_clean.append(transaction_clean)” had to be on the first loop not the second one (aka I just spent like 2 hours stuck on this code cause of a single space button, most definitely “not” discouraged at all, bruh)

You’re going to make “stupid” mistakes. The key is in narrowing down the source and fixing them.
Generally you’d do something like starting somewhere in the middle of things, see if the problem exists at that point, if yes look earlier, if no, look later.

The nature of your problem says a lot about what could cause it as well. If everything’s duplicated then it’s got to do with a loop, and if it’s 4 times, then what have you got 4 of?

Yup, debugging, I coulda have found it much earlier if I didn’t just keep glossing over the text without printing stuff out… But at the same time, I kinda did learn a lesson here hah.

And yup that was the main thing that uncovered it for me, once I figured out that 4x problem, it wasn’t long coming (tho I did spend like 10 minutes afterwards, cause legit there was no 4 to be seen nowhere, but the data itself was now split into lists of 4, and that’s why it was 4x)

Thanks for the answer tho!

1 Like

btw, this:

can be replaced with splitting each entry on & (regardless of whether there is one)
and then concatenating all those results.

actually, the reason I don’t like it too much is the amount of nesting, it’s a bit … intertwined, difficult.

another way would be to first split them into two lists, has, and hasn’t &, then split the has’s and concat … this would be a more sequential approach, solving one problem at a time, feeding the result into the next process.
…except, yeah, the path for the has’s would apply to the hasn’t’s as well
you’ve got similar duplication of paths in your code - it doesn’t matter if the & exists or not, you can split it regardless, and that saves you the condition + the code for it not existing
similarly, this will never be satisfied: if splat == []:
Deleting those things with no other changes leaves that code as:

for i in thread_sold:
  splat = i.split("&")
  for u in splat:

Which is split into concat… actually, python doesn’t really have an easily accessible concat for lists, I don’t think, so I guess you’re stuck with at least one of those loops.

Thank you lots for the answers! :slight_smile:

I’ve done the same, many times. Don’t beat yourself up. Everytime you make one of these “obvious” mistakes, you are training yourself how to spot them next time.

If you did this again, you would spot it in 2 mins.