Help with thread shed

I’m having trouble somewhere in the last few steps. When I try to get through step 22 the output is formatted like
24
Thread Shed sold None threads of red thread today.
even though I was able to get the value for counting white threads a couple of steps before.

Here is my full code just in case:
daily_sales_replaced = daily_sales.replace(“;,;”, “;”)

daily_transactions = daily_sales_replaced.split(“,”)
#print(daily_transactions)

daily_transactions_split =
for line in daily_transactions:
daily_transactions_split.append(line.split(“;”))
#print(daily_transactions_split)

transactions_clean =
for line in daily_transactions_split:
line_clean =
for element in line:
line_clean.append(element.replace(“\n”, “”).strip())
transactions_clean.append(line_clean)
#print(transactions_clean)

customers =
sales =
thread_sold =

for line in transactions_clean:
customers.append(line[0])
sales.append(line[1])
thread_sold.append(line[2])

#print(customers)
#print(sales)
#print(thread_sold)

total_sales = 0
for e in sales:
price = float(e.strip(“$”))
total_sales += price
#print(total_sales)

#print(thread_sold)

thread_sold_split =
for e in thread_sold:
if e.count(“&”) > 0:
e_new = e.split(“&”)
for i in range(len(e_new)):
thread_sold_split.append(e_new[i])
else:
thread_sold_split.append(e)

#print(thread_sold_split)

def color_count(color):
count = 0
for e in thread_sold_split:
if e == color:
count += 1
print(count)
#color_count(“white”)

colors = [“red”, “yellow”, “green”, “white”, “black”, “blue”, “purple”]
for color in colors:
print(“Thread Shed sold {a} threads of {b} thread today.”.format(a = color_count(color), b = color))

1 Like

it looks like your color_count function is missing a return
return count
should be at the end of the function
instead of
print(count)

I’m gettin none, I don’t what I di wrong, I have changed the name of the argument, changed from double to single quotation , checked for indentions, I would truly appreciate a hint

daily_sales_replaced = daily_sales.replace(‘;,;’, ‘+’)
daily_transactions = daily_sales_replaced.split(‘,’)
#print(daily_transactions)

daily_transactions_split =
for transaction in daily_transactions:
daily_transactions_split.append(transaction.split(‘+’))
#print(daily_transactions_split)

transactions_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data_point in transaction:
transaction_clean.append(data_point.replace(‘\n’, ‘’).strip(’ '))
transactions_clean.append(transaction_clean)
#print(transactions_clean)

customers =
sales =
thread_sold =

for transaction in transactions_clean:
customers.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])

#print(customers)
#print(sales)
#print(thread_sold)

total_sales = 0

for sale in sales:
total_sales += float(sale.strip(‘$’))
#print(total_sales)

#print(thread_sold)

thread_sold_split =
for sales in thread_sold:
for color in sale.split(‘$’):
thread_sold_split.append(color)

def color_count(color):
total = 0
for color in thread_sold_split:
if color == thread_sold:
total += 1
return total()
print(color_count(‘white’))

in the color_count function
you have return total()
but total is not a function.
It should be return total with no ()

When you post your code, please use the </> button and put your code on lines between the ``` and ``` so that the post keeps the formatting of the code (like the indentation of each line).

1 Like

still giving me none

def color_count(color):

total = 0
for color in thread_sold_split:

if color == thread_sold:

total += 1
return total
print(color_count(‘white’))

check the indentation
The return should not be in the loop.

def color_count(color):
  total = 0
  for thread_sold in thread_sold_split:
    if color == thread_sold:
      total += 1
  return total

print(color_count('white'))

Notice that I did not use color for iterating through the for-loop because color is already used as the parameter for the function.
I used thread_sold for the for-loop instead.

But, if thread_sold_split is a 2D list (a list of lists) then you may need an inner loop too
or somehow access an element of the inner list.

def color_count(color):
  total = 0
  for sales in thread_sold_split:
    for thread_sold in sales:
      if color == thread_sold:
        total += 1
  return total
1 Like

Hey I am having an issue this project as well. I have followed along with our buddy Matt. I have noticed that when I am beginning step #6.

We are supposed to iterate through daily_transaction and for each transaction, split the string around whatever character you replaced “;,;” with. In my case, as well as the video the + plus was selected.

My code is as follows (only putting relevant code in)

transactions_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data_point in transaction:
transaction_clean.append(data_point.replace(“\n”, “”).strip(" "))
transactions_clean.append(transaction_clean)
print(transactions_clean)

So the issue with this is that instead of cleaning up white space, it also created several entries of each sale. I have attempted to make changes to my code and I even watched the video while doing the project. For some reason, it keeps creating duplicate records

We cannot tell by looking, but just make sure that line is NOT in the inner loop.

1 Like

transactions_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data_point in transaction:
transaction_clean.append(data_point.replace(“\n”, “”).strip(" "))
transactions_clean.append(transaction_clean)
print(transactions_clean)

I reread your post, and yes, I was keeping that line indented. thanks

1 Like

This outputs 55, not 28

why doesnt it return 28?

if you have a 2D list that looks something like this:
[‘white’, [‘white’, ‘blue’], [‘white’, ‘blue’], ‘white’, [‘white’, ‘yellow’]…etc.]

then you have to iterate through all lists.
in the first loop, you iterate through the singular colors and count them.
in the second loop you iterate through the lists within the list.

i couldn’t find a way to put it in a nested loop that worked, so i made two loops:

  count = 0
  for col in thread_sold_split:
    if col == color:
      count = count + 1
  for i in thread_sold_split:
    for j in i:
      if j == color:
        count = count + 1
  return count

the first loop only adds singular colors that i’m looking for and adds them to the count.
the second loop puts the singular colors into letters ( ‘white’ will become ‘w’, ‘h’, ‘i’, ‘t’, ‘e’) and won’t be counted, but the elements of the list within the list will be iterated through and added to the count if there’s a match.

the example with ‘white’ gave me 6 matches in the first loop, and 22 in the second loop, which adds up to 28.

1 Like