Getting 112 as the number of “whites” in step 20 instead of 28

Hi, I am still getting number 112 as the number of “whites” in step 20 instead of 28. Can you please help?

daily_sales_replaced = daily_sales.replace(";,;", "+")
daily_transactions = daily_sales_replaced.split(",")

daily_transactions_split = []
for i in daily_transactions:
  daily_transactions_split.append(i.split("+"))

transactions_clean = []
for trans in daily_transactions_split:
  transaction_clean = []
  for data in trans: 
    transaction_clean.append(data.replace("\n"," ").strip(" "))
    transactions_clean.append(transaction_clean)

customers = []
sales = []
thread_sold = []

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

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

thread_sold_split = []
for sale in thread_sold:
  for color in sale.split("&"):
    thread_sold_split.append(color)

def color_count(color):
  color_total = 0
  for thred_color in thread_sold_split:
    if color == thred_color:
      color_total += 1
  return color_total
print(color_count("white"))

colors = ['red','yellow','green','white','black','blue','purple']

for color in colors:
  print("Thread Shed sold {0} threads of {1} threads today.".format(color_count(color),color))
5 Likes

please remind me of what step 20 was? It looks like your code follows almost exactly what the video walk through had. My code was a bit different, albeit more sloppy, but it worked. It’s possible just by looking at your code that you might have a wrong indent someplace or called the wrong variable.

# Start coding below!
daily_sales_replaced = daily_sales.replace(';,;', '+')
daily_transactions = daily_sales_replaced.split(',')

#print(daily_transactions)

daily_transactions_split = []
for daily_transaction in daily_transactions:
  daily_transactions_split.append(daily_transaction.split('+'))

#print(daily_transactions_split)

transactions_clean = []
for daily_transactions in daily_transactions_split:
  transaction_clean = []
  for transaction in daily_transactions:
    transaction_clean.append(transaction.strip('\n').strip())
  transactions_clean.append(transaction_clean)
    
    
#print(transactions_clean)

customers = []
sales = []
thread_sold = []

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

total_sales = 0
for sale in sales:
  total_sales += float(sale.strip('$'))

#print(total_sales)

thread_sold_split = []
for sale in thread_sold:
  for color in sale.split('&'):
    thread_sold_split.append(color)
  
#print(thread_sold_split)

def color_count(color):
  color_total = 0
  for color_sold in thread_sold_split:
    if color == color_sold:
      color_total += 1
  return print(color_total)
    
  
#color_count("white")

colors = ['red','yellow','green','white','black','blue','purple']

for color in colors:
  print("Thread Shed sold {0} threads of {1} thread today."
        .format(color_count(color), color)
  )

The output of the last loop is as follows:
24
Thread Shed sold None threads of red thread today.
34
Thread Shed sold None threads of yellow thread today.
30
Thread Shed sold None threads of green thread today.
28
Thread Shed sold None threads of white thread today.
26
Thread Shed sold None threads of black thread today.
22
Thread Shed sold None threads of blue thread today.
17
Thread Shed sold None threads of purple thread today.

Why is that? It is the same as the solution in the walk through video - or am I missing something?

It looks like in this part they used transaction_clean.aooend(data_point.REPLACE(“\n”, " “).strip(” ")

You also have two empty lists here

I am pretty new to coding, so I can’t really advise you on logic but I can help proof reading your code:)

Thanks, but that was not my question - I struggle with the last loop only. It does not come out correct.

I am aware that they used .replace - I think it’s one of those things when more solutions are possible and I preferred mine so I kept it.

The two empty lists are neccessary there as I am adding to them later.

That line is returned the return value from print(), which is None.

return color_total
2 Likes

This same issue boggled me for hours. Thank you for your attention to detail!

1 Like

Thank you for this! Indentation in Python is a nightmare :slight_smile:

1 Like

2 posts were split to a new topic: Don’t really grasp where does the difference come from?

84 posts were split to a new topic: Why is it that my for loop is not iterating through the whole list?