Thread Shed Challenge

#1

exercise link:
https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/projects/thread-shed

transactions_clean = []
for tr in daily_transactions_split:
  for space in range(len(tr)):
    tr[space] = tr[space].strip()
    transactions_clean.append(tr)   

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

for trans in transactions_clean:
  if trans[0] in trans:
    name = trans[0]
    customers.append(name)
    
for sale in transactions_clean:
  if sale[1] in sale:
    price = sale[1]
    sales.append(price)

for thread in transactions_clean:
  if thread[2] in thread:
    thread_type = thread[2]
    thread_sold.append(thread_type)

When I print my individual lists to the console, the individual lists print out a list of each name, sale, and thread four times in what seems like an almost never ending repetition of the individual lists.

['white', 'white', 'white', 'white', 'white&blue', 'white&blue', 
'white&blue', 'white&blue', 'white&blue', 'white&blue', 'white&blue',
 'white&blue', 'white', 'white', 'white', 'white', 'white&yellow',
 'white&yellow', 'white&yellow', .....]```

Not sure if this is caused by the previous transaction_clean code where each individual transaction was stripped of it’s white space and placed into individual transactions lists, but the lists were repeated three times.


[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Edith Mcbride',
 '$1.21', 'white', '09/15/17'], ['Edith Mcbride', '$1.21', 'white', 
'09/15/17'], 

I am fairly sure this was not the intended output for the desired code and was wondering if anyone can help point me in the write direction.

1 Like
#2

The above line is nested inside two loops, hence the repetition.

2 Likes
#3

Thank you!

It is small things like this I notice myself looking over. I appreciate the reply.

1 Like
#4

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

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.

#6
# 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?

#7

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:)

#8

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.

#9

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

return color_total
1 Like
#10

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

#11

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

1 Like