Codecademy Forums

Thread Shed Challenge

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

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

5 Likes

Thank you!

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

1 Like

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

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
1 Like

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

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

1 Like

Hi! I have finished the excercise and got the correct prints before looking up the solutions. The way I have done some of the steps is different than what the description was suggesting to do. In particular I have been trying to do step 11 as suggested, but was failing and decided to do it like this:

customers = transactions_clean[0::4]
sales =transactions_clean[1::4]
thread_sold = transactions_clean[2::4]

After finishing the excercise I have skimmed through the YouTube video and tried doing:
customers =
sales =
thread_sold =
for transaction in transactions_clean:
customers.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])

But what I get printed after this are the first, second and third characters in transaction. So I guess it is string vs list problem, but I don’t really grasp where does the difference come from?

This is the full code I wrote for the excercise(with the 2nd method on step 11 omitted by #):

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 i in daily_transactions_split:
  for a in i:
    transactions_clean.append(a.strip())
customers = transactions_clean[0::4]
sales =transactions_clean[1::4]
thread_sold = transactions_clean[2::4]
#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 value
total_sales = 0
for i in sales:
  value = float(i.strip("$"))
  total_sales += value
#print(total_sales)
#How much thread of specific color
thread_sold_split = []
for i in thread_sold:
  if i.find("&") == -1:
    thread_sold_split.append(i)
  else:
    b = i.split("&")
    for c in b:
    	thread_sold_split.append(c)
#print(thread_sold_split)
def color_count(color):
  count = 0
  for i in thread_sold_split:
    if i == color:
      count += 1
  return count
#print(color_count("white"))

colors = ['red','yellow','green','white','black','blue','purple']
for i in colors:
  x=color_count(i)  
  print("Thread Shed sold {count} thread of {color} thread today.".format(color = i, count = x))
    

Is what I wrote wrong in some way?
Thanks

1 Like

No, you did a great job. Just make sure that you understand how the code in the video works.

In Python, the magic of for-in means that we often do not need to use indexing at all, or if we use it, we often are able to omit one level of indexing in nested lists. It’s rare to see slicing used to break them down. (In plain Python, that is; in Numpy & Pandas it’s quite common!)


Anyway,

Yes.

“Your” transactions_clean list is a list of strings:
['Edith Mcbride', '$1.21', 'white', '09/15/17', 'Herbert Tran'... etc. ]
so one transaction from transactions_clean is a string:
'Edith Mcbride'
… making transaction[0], transaction[1], etc, separate characters, ‘E’, ‘d’, ‘i’, etc.

Whereas “their” transactions_clean list is a list of lists of strings:
[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran',...etc], etc]
… so one transaction is a list:
[['Edith Mcbride', '$1.21', 'white', '09/15/17']
… making transaction[0] a string: 'Edith Mcbride'

1 Like

Hello! I have been coming to this forum often to check answers and problems when i get stuck and just to learn other techniques others may have. Up until now i have not posted anything, but i am stuck on the Thread Shed Challenge so this is my first question in the forums. I wanna thank you all for your help already. So, when i run my code all i get is

[[‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’]]

And This is my code:

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

Why is it that my for loop is not iterating through the whole list and instead it is only giving me one customer, price, and thread? I have been stuck on this problem for 2 days trying to work myself out of it but now it’s time to suck it up and ask for help!!! Can anyone please explain this to me??

We cannot tell the code structure here, but one suspects both of these appends are inside the loop. The latter should NOT be in the loop, but after it.

I followed the guy on the video solution for the problem after i got stuck. So it is the video solution provided for the problem.

for data_point in transaction_clean:
    transaction_clean.append(data_point.replace("\n", “”).strip(" "))

transactions_clean.append(transaction_clean)

Does your code look like that?

 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)

No. This is how i coded it after what the video solution told me to do but something is wrong somewhere because i still haven’t got past #12 on this problem.

As suspected, both appends are inside the inner loop;

Well, i get a little confused (okay more than a little) with for loops. Can you explain that to me a little more and better? Excuse me, im a newbie to coding and im starting from ground zero. I’ve done other for loops and haven’t had as difficult a time as i am on this one.