Stuck on Step 19 of Thread Shed

Hello! I’ve been working on Thread Shed and for the life of me, I don’t know what I’m doing wrong on step 19. I checked the video, and my code matched up, but it keeps returning 112 instead of 28. Here is the portion of my code partaining to step 19:

daily_sales = \
"""Edith Mcbride   ;,;$1.21   ;,;   white ;,; 
09/15/17   ,Herbert Tran   ;,;   $7.29;,; 
white&blue;,;   09/15/17 ,Paul Clarke ;,;$12.52 
;,;   white&blue ;,; 09/15/17 ,Lucille Caldwell   
;,;   $5.13   ;,; white   ;,; 09/15/17,
Eduardo George   ;,;$20.39;,; white&yellow 
;,;09/15/17   ,   Danny Mclaughlin;,;$30.82;,;   
purple ;,;09/15/17 ,Stacy Vargas;,; $1.85   ;,; 
purple&yellow ;,;09/15/17,   Shaun Brock;,; 
$17.98;,;purple&yellow ;,; 09/15/17 , 
Erick Harper ;,;$17.41;,; blue ;,; 09/15/17, 
Michelle Howell ;,;$28.59;,; blue;,;   09/15/17   , 
Carroll Boyd;,; $14.51;,;   purple&blue   ;,;   
09/15/17   , Teresa Carter   ;,; $19.64 ;,; 
white;,;09/15/17   ,   Jacob Kennedy ;,; $11.40   
;,; white&red   ;,; 09/15/17, Craig Chambers;,; 
$8.79 ;,; white&blue&red   ;,;09/15/17   , Peggy Bell;,; $8.65 ;,;blue   ;,; 09/15/17,   Kenneth Cunningham ;,;   $10.53;,;   green&blue   ;,; 
09/15/17   ,   Marvin Morgan;,;   $16.49;,; 
green&blue&red   ;,;   09/15/17 ,Marjorie Russell 
;,; $6.55 ;,;   green&blue&red;,;   09/15/17 ,
Israel Cummings;,;   $11.86   ;,;black;,;  
09/15/17,   June Doyle   ;,;   $22.29 ;,;  
black&yellow ;,;09/15/17 , Jaime Buchanan   ;,;   
$8.35;,;   white&black&yellow   ;,;   09/15/17,   
Rhonda Farmer;,;$2.91 ;,;   white&black&yellow   
;,;09/15/17, Darren Mckenzie ;,;$22.94;,;green 
;,;09/15/17,Rufus Malone;,;$4.70   ;,; green&yellow 
;,; 09/15/17   ,Hubert Miles;,;   $3.59   
;,;green&yellow&blue;,;   09/15/17   , Joseph Bridges  ;,;$5.66   ;,; green&yellow&purple&blue 
;,;   09/15/17 , Sergio Murphy   ;,;$17.51   ;,;   
black   ;,;   09/15/17 , Audrey Ferguson ;,; 
$5.54;,;black&blue   ;,;09/15/17 ,Edna Williams ;,; 
$17.13;,; black&blue;,;   09/15/17,   Randy Fleming;,;   $21.13 ;,;black ;,;09/15/17 ,Elisa Hart;,; $0.35   ;,; black&purple;,;   09/15/17   ,
Ernesto Hunt ;,; $13.91   ;,;   black&purple ;,;   
09/15/17,   Shannon Chavez   ;,;$19.26   ;,; 
yellow;,; 09/15/17   , Sammy Cain;,; $5.45;,;   
yellow&red ;,;09/15/17 ,   Steven Reeves ;,;$5.50   
;,;   yellow;,;   09/15/17, Ruben Jones   ;,; 
$14.56 ;,;   yellow&blue;,;09/15/17 , Essie Hansen;,;   $7.33   ;,;   yellow&blue&red
;,; 09/15/17   ,   Rene Hardy   ;,; $20.22   ;,; 
black ;,;   09/15/17 ,   Lucy Snyder   ;,; $8.67   
;,;black&red  ;,; 09/15/17 ,Dallas Obrien ;,;   
$8.31;,;   black&red ;,;   09/15/17,   Stacey Payne 
;,;   $15.70   ;,;   white&black&red ;,;09/15/17   
,   Tanya Cox   ;,;   $6.74   ;,;yellow   ;,; 
09/15/17 , Melody Moran ;,;   $30.84   
;,;yellow&black;,;   09/15/17 , Louise Becker   ;,; 
$12.31 ;,; green&yellow&black;,;   09/15/17 ,
Ryan Webster;,;$2.94 ;,; yellow ;,; 09/15/17 
,Justin Blake ;,; $22.46   ;,;white&yellow ;,;   
09/15/17,   Beverly Baldwin ;,;   $6.60;,;   
white&yellow&black ;,;09/15/17   ,   Dale Brady   
;,;   $6.27 ;,; yellow   ;,;09/15/17 ,Guadalupe Potter ;,;$21.12   ;,; yellow;,; 09/15/17   , 
Desiree Butler ;,;$2.10   ;,;white;,; 09/15/17  
,Sonja Barnett ;,; $14.22 ;,;white&black;,;   
09/15/17, Angelica Garza;,;$11.60;,;white&black   
;,;   09/15/17   ,   Jamie Welch   ;,; $25.27   ;,; 
white&black&red ;,;09/15/17   ,   Rex Hudson   
;,;$8.26;,;   purple;,; 09/15/17 ,   Nadine Gibbs 
;,;   $30.80 ;,;   purple&yellow   ;,; 09/15/17   , 
Hannah Pratt;,;   $22.61   ;,;   purple&yellow   
;,;09/15/17,Gayle Richards;,;$22.19 ;,; 
green&purple&yellow ;,;09/15/17   ,Stanley Holland 
;,; $7.47   ;,; red ;,; 09/15/17 , Anna Dean;,;$5.49 ;,; yellow&red ;,;   09/15/17   ,
Terrance Saunders ;,;   $23.70  ;,;green&yellow&red 
;,; 09/15/17 ,   Brandi Zimmerman ;,; $26.66 ;,; 
red   ;,;09/15/17 ,Guadalupe Freeman ;,; $25.95;,; 
green&red ;,;   09/15/17   ,Irving Patterson 
;,;$19.55 ;,; green&white&red ;,;   09/15/17 ,Karl Ross;,;   $15.68;,;   white ;,;   09/15/17 , Brandy Cortez ;,;$23.57;,;   white&red   ;,;09/15/17, 
Mamie Riley   ;,;$29.32;,; purple;,;09/15/17 ,Mike Thornton   ;,; $26.44 ;,;   purple   ;,; 09/15/17, 
Jamie Vaughn   ;,; $17.24;,;green ;,; 09/15/17   , 
Noah Day ;,;   $8.49   ;,;green   ;,;09/15/17   
,Josephine Keller ;,;$13.10 ;,;green;,;   09/15/17 ,   Tracey Wolfe;,;$20.39 ;,; red   ;,; 09/15/17 ,
Ignacio Parks;,;$14.70   ;,; white&red ;,;09/15/17 
, Beatrice Newman ;,;$22.45   ;,;white&purple&red 
;,;   09/15/17, Andre Norris   ;,;   $28.46   ;,;   
red;,;   09/15/17 ,   Albert Lewis ;,; $23.89;,;   
black&red;,; 09/15/17,   Javier Bailey   ;,;   
$24.49   ;,; black&red ;,; 09/15/17   , Everett Lyons ;,;$1.81;,;   black&red ;,; 09/15/17 ,   
Abraham Maxwell;,; $6.81   ;,;green;,;   09/15/17   
,   Traci Craig ;,;$0.65;,; green&yellow;,; 
09/15/17 , Jeffrey Jenkins   ;,;$26.45;,; 
green&yellow&blue   ;,;   09/15/17,   Merle Wilson 
;,;   $7.69 ;,; purple;,; 09/15/17,Janis Franklin   
;,;$8.74   ;,; purple&black   ;,;09/15/17 ,  
Leonard Guerrero ;,;   $1.86   ;,;yellow  
;,;09/15/17,Lana Sanchez;,;$14.75   ;,; yellow;,;   
09/15/17   ,Donna Ball ;,; $28.10  ;,; 
yellow&blue;,;   09/15/17   , Terrell Barber   ;,; 
$9.91   ;,; green ;,;09/15/17   ,Jody Flores;,; 
$16.34 ;,; green ;,;   09/15/17,   Daryl Herrera 
;,;$27.57;,; white;,;   09/15/17   , Miguel Mcguire;,;$5.25;,; white&blue   ;,;   09/15/17 ,   
Rogelio Gonzalez;,; $9.51;,;   white&black&blue   
;,;   09/15/17   ,   Lora Hammond ;,;$20.56 ;,; 
green;,;   09/15/17,Owen Ward;,; $21.64   ;,;   
green&yellow;,;09/15/17,Malcolm Morales ;,;   
$24.99   ;,;   green&yellow&black;,; 09/15/17 ,   
Eric Mcdaniel ;,;$29.70;,; green ;,; 09/15/17 
,Madeline Estrada;,;   $15.52;,;green;,;   09/15/17 
, Leticia Manning;,;$15.70 ;,; green&purple;,; 
09/15/17 ,   Mario Wallace ;,; $12.36 ;,;green ;,; 
09/15/17,Lewis Glover;,;   $13.66   ;,;   
green&white;,;09/15/17,   Gail Phelps   ;,;$30.52   
;,; green&white&blue   ;,; 09/15/17 , Myrtle Morris 
;,;   $22.66   ;,; green&white&blue;,;09/15/17"""

#------------------------------------------------
# Start coding below!
daily_sales_replaced = daily_sales.replace(";,;", ";")
daily_transactions = daily_sales_replaced.split(",")
daily_transactions_split = []
for sale in daily_transactions:
  split_sale = sale.split(";")
  daily_transactions_split.append(split_sale)
transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for data_point in transaction:
    transaction_clean.append(data_point.strip())
    transactions_clean.append(transaction_clean)
customers = []
sales = []
thread_sold = []
for sale in transactions_clean:
  customers.append(sale[0])
  sales.append(sale[1])
  thread_sold.append(sale[2])

total_sales = 0
for sale in sales:
  no_sign = sale.strip("$")
  total_sales += float(no_sign)
thread_sold_split = []
for sale in thread_sold:
  if "&" not in sale:
    thread_sold_split.append(sale)
  else:
    single_colour = sale.split("&")
    for colour in single_colour:
      thread_sold_split.append(colour)
def colour_count(colour):
  colour_total = 0
  for sale in thread_sold_split:
    if colour == sale:
      colour_total += 1
  return colour_total

print(colour_count("white"))

Let me know if you need more of my code to see what went wrong! Thank you in advance!

1 Like

HI @varianthealchemist.

That’s a value I’m assuming is exactly four times larger than it should be. Have you checked the contents of this list? What process in your code might be causing this specific increase?

2 Likes

First: the general guideline that in these bigger projects it’s always good to use print lines at almost every step to verify you’re getting the right results without piling a mountain of things together. That even means double check your for loops for what they spit out by default.

Then in a technical bit, don’t forget that with for loops there’s a difference between for i in list, and for i in range(0,len(list)). This can often be instrumental in unlocking issues… in my opinion

1 Like

I just rewrote my entire code from scratch, printing at every step. I can find no apparent problems until Step 19. I tried using your suggestion of for for i in range(0,len(thread_sold_split), but that just returned 0. What else can I do to debug my code?

Let’s try to compare. You’ve flipped enough around it won’t do to get too frustrated.

Apologies for any inelegant code, I’m just learning too!

daily_sales_replaced = []
daily_sales_replaced = daily_sales.replace(';,;', ' @ ')

daily_transactions = []
daily_transactions = daily_sales_replaced.split(',')

daily_transactions_split = [daily_transactions[i].split('@') for i in range(0,len(daily_transactions))]

transactions_clean = []

for transaction in range(0, len(daily_transactions_split)):
	for detail in range(0, len(daily_transactions_split[transaction])):
		
		transactions_clean.append(daily_transactions_split[transaction][detail].strip())

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


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


total_sales = 0

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

threads_sold_split = []

for threads in range(0, len(thread_sold)):
	
	threads_sold_split.append(thread_sold[threads].split('&'))

def color_count(color):
	counter = 0
	for item in range(0, len(threads_sold_split)):
		if color in threads_sold_split[item]:
			counter += 1
	return counter


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

I tried experimentally pasting portions of your code into mine and still get either 112 or 0 as my answer. In case my code has changed any since my first post, here it is again:

daily_sales_replaced = daily_sales.replace(";,;", ";")
daily_transactions = daily_sales_replaced.split(",")
daily_transactions_split = []
for sale in daily_transactions:
  split_sale = sale.split(";")
  daily_transactions_split.append(split_sale)
transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for data_point in transaction:
    transaction_clean.append(data_point.strip())
    transactions_clean.append(transaction_clean)
customers = []
sales = []
thread_sold = []
for sale in transactions_clean:
  customers.append(sale[0])
  sales.append(sale[1])
  thread_sold.append(sale[2])

total_sales = 0
for sale in sales:
  no_sign = sale.strip("$")
  total_sales += float(no_sign)
thread_sold_split = []
for sale in thread_sold:
  if "&" not in sale:
    thread_sold_split.append(sale)
  else:
    single_colour = sale.split("&")
    for colour in single_colour:
      thread_sold_split.append(colour)
def colour_count(colour):
  colour_total = 0
  for sale in thread_sold_split:
    if colour == sale:
      colour_total += 1
  return colour_total

print(colour_count("white"))

I think I found part of the problem. If you print out what I’m about to paste, you’ll see that although you do isolate names, the way you call them makes them repeat a bunch. I’m sure it would be the problem with the numbers as well. Remember the range function (method?) has that optional third parameter to sequence. That was the big clue for unlocking this one for me personally.

I personally just hand-write mini-lists of 4-6 items to see what happens in these situations to have better technical control when getting into these.

There might be a more elegant way to write my code, no doubt… I’m still improving too!

compare this line:

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

with this one

for transaction in range(0, len(transactions_clean), 4):
	customers.append(transactions_clean[transaction])
for transaction in range(1, len(transactions_clean), 4):
	sales.append(transactions_clean[transaction])
for transaction in range(2, len(transactions_clean), 4):
	thread_sold.append(transactions_clean[transaction])
1 Like

YES! :smile: It finally returns 28!

Huzzahh!

Another thing you can do with classes, function, and methods is use the help and dir commands like

help(str)

will output all the documentation for string and

dir(some class object)

will output attributes and functions of said object.

I find this extremely useful when up against a wall because it can help give me ideas to solutions I might not have thought of.

A bug doesn’t suddenly appear out of nowhere, it’s something you can observe being carried out.

You’d find a location in the code, as small as possible, where things start out in a way you think is correct, but ends up in a way you think is wrong. The bug would be somewhere in that location. Then you’d look at what was carried out there. It’s important that you’re able to see correct input and incorrect output, otherwise you don’t know that this is where the problem is.

The input for that project is rather large, you could use a small part of it for debugging so that the output isn’t overwhelming, so that it’s small enough that you can see what things should be or shouldn’t be.

yeah, that.

This is redundant by the way. Consider what happens if you split without it being there. It’s not a special case.


take a look at collections.Counter


You have many loops through the entries, you really only need to first iterate through it once to clean each one up, then you would use sum for the total sale and Counter to get color frequencies.

namedtuple is handy here as well, you can do something like this:

Transaction = namedtuple("Transaction", ["sale", "threads"])

and then the first thing you would do is to turn each transaction into a Transaction,

transactions = []
for transaction in daily_sales.replace(";,;", ";").split(","):
    _name, sale, threads, _date = map(str.strip, transaction.split(";"))
    threads = threads.split("&")
    dollars, cents = sale.strip("$").split(".")
    transactions.append(Transaction(100 * int(dollars) + int(cents), threads))

after that you would have a list of transactions which would be super easy to work with, for example:

total_sale = sum(t.sale for t in transactions)

You don’t want three lists. You want one list of Transactions

1 Like

Glad you found a solution. Since this is already solved I’d just note in the future that your original error was indenation. I’m assuming that transactions_clean was not intended to be a part of that second loop.

for transaction in daily_transactions_split:
  transaction_clean = []
  for data_point in transaction:
    transaction_clean.append(data_point.strip())
    transactions_clean.append(transaction_clean)  # <-- Issue.
    # The above line probably shouldn't be in both loops. Just the outer one.

If you fire some print statements in there you’ll see one of the curiosities about mutability in Python regarding the length of the list. What you end up with is repeated references to the same list object which itself changes per iteration.

I think the exercise itself asks for three separate lists but the namedtuple idea is so much better!!! I’m glad I’m still revisiting thins. Plenty of things to clean up.

Yeah because the instructions describe what code to write rather than what it should do. Refactoring the described code is often a better exercise than the project itself.

2 Likes