Python 3 _ Thread-shed

Hi I have a question regarding below course

[https://www.codecademy.com/courses/learn-python-3/projects/thread-shed]

So, at task 8, I realized that I was not getting the expected result (list in list) because I defined a new list outside of the loop.
I quite don’t understand why is that…
What makes it different?

//WRONG//
transactions_clean =
transaction = []
for i in daily_transactions_split:
for k in i:
transaction.append(k.strip())
transactions_clean.append(transaction)

//CORRECT//
transactions_clean =
for i in daily_transactions_split:
transaction = []
for k in i:
transaction.append(k.strip())
transactions_clean.append(transaction)

3 Likes

In the second example we are re-using the list, starting from empty on each iteration.

2 Likes

Hi I wrote the following code for “Thread Shed” but the counting that I receiving is not even close to the counts in the example (and in the video) I don’t understand what’s wrong in my counter function
this is the code:
daily_sales_replaced = daily_sales.replace(’;,;’,’;’)
daily_transaction = daily_sales_replaced.split(’,’)
#print (daily_transaction)
daily_transactions_split =
for tran in daily_transaction:
daily_transactions_split.append(tran.split(’;’))
#print(daily_transactions_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 price in sales:
total_sales+=float(price.strip(’$’))

#print (total_sales)
#print (thread_sold)

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):
total_colors = 0
for tred_color in thread_sold_split:
if tred_color == color:
total_colors += 1
return total_colors

#print(color_count(‘white’))

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

for color in colors:
print (“Thread Shed sold {} threads of {} threads today.”.format(color_count(color),color))

and these are the results:
Thread Shed sold 96 threads of red threads today.
Thread Shed sold 136 threads of yellow threads today.
Thread Shed sold 120 threads of green threads today.
Thread Shed sold 112 threads of white threads today.
Thread Shed sold 104 threads of black threads today.
Thread Shed sold 88 threads of blue threads today.
Thread Shed sold 68 threads of purple threads today.

If we look closely the above output amounts are 4 times those expected. That should point to the issue.

1 Like

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)

Thanks a lot!!!

1 Like

Python is very dependent upon block syntax demarcated with indentation. In this exercise we learn to back out of a lower scope block, back into the parent scope block. This would be akin to a times table, row on row.

Multiples in the outputs that equate for all arguments points to nested loops.


On the above code, consider,

We have color information in the thread_sold_split list. Rather than arbitrarily declare the list, it is possible to create an index of unique colors using the set() constructor.

colors = set(thread_sold_split)

We can do this only if it’s certain the list we’re working with is comprised of color names. This is a point to look backwards through code to make sure it is always going to happen.

Now that we have a colors object we can iterate it, with no nesting.

def sales_report(x):
    for c in colors:  # the global is immutable in this setting
        print ('Color: {}\nCount: {}'.format(c, x.count(c)))
    return 1
>>> sales_report(thread_sold_split)
Color: blue
Count: 22
Color: green
Count: 30
Color: yellow
Count: 34
Color: purple
Count: 17
Color: white
Count: 28
Color: red
Count: 24
Color: black
Count: 26
1
>>> 
1 Like

We’re free to create our own count() function if the exercise expects. It’s moot if we know how to do that already; the built-in is something that we understand.

1 Like