Thread Shed Challenge

When we are taught for loops, it is described something like this…

for element in iterable:

It can be any iterable, a string, a list, a tuple, a set, a dict, or an iterator such as map, filter, zip, etc.

We learned that str.split() returns a new list, so as it appears here is also iterable, so,

for x in str.split(separator):

where separator can be any character string, hence we see,

for color in sale.split('&'):

It is not necessary in this case to assign it to any variable since it is a list object, already. Nothing is keeping us from creating an intermediate variable, and then using it, but on refactoring we would eventually revert to the above.

sale_split_on_ampersand = sale.split('&')

for color in sale_split_on_ampersand:

Blockquote
My code for this, is there any way to make it cleaner ?

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]


total_sales = 0
for i in sales:
  value = float(i.strip("$"))
  total_sales += value
thread_sold_a = []
thread_sold_split = []
for i in thread_sold:
  if i.find("&") == True:
    thread_sold_split.append(i)
  else:
    for a in i.split("&"):
      thread_sold_split.append(a)
#print(thread_sold_split)
      
def color_count(color):
  color_count = 0
  for a in thread_sold_split:
    if a == color:
      color_count += 1
  return color_count

#print(color_count("white"))  

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

for i in colors:
  print(f"Thread Shed sold {color_count(i)} threads of {i} thread today")
    

If you think of your program like a big math expression to be simplified with parts of it cancelling out and removed … then yeah there’s a fair bit you can get rid of in that.

For example, a lot of your loops are iterating through the same thing. An entry from the input. They could all be the same loop.

Splitting on “&” can be done regardless of whether there’s a “&” in it. You have two separate cases for that, they’re the same thing.

This really helped thanks.

1 Like

I am stuck at #11 where it’s asking for customer, sales, and thread_sold.
Does anyone here have the solution to this, or the entire project?

I have managed to produced transactions_clean to be a list of lists of strings.
e.g.

print(transactions_clean[0]) 
['Edith Mcbride   ;$1.21   ;   white ; 09/15/17']
print(transactions_clean[99])
['Myrtle Morris \n;   $22.66   ; green&white&blue;09/15/17']

Is this what we are suppose to have right up to this point?

If not, then what is transactions_clean suppose to look like?

This is what I get…

['Edith Mcbride', '$1.21', 'white', '09/15/17']
# ...
['Myrtle Morris', '$22.66', 'green&white&blue', '09/15/17']

It is still possible for you to finish cleaning up the content you have to this point.

transactions_cleaner = []
for x in transactions_clean:
    transactions_cleaner.append(x.split(';').strip())

print (transactions_cleaner[0])

See what that looks like. In review, how could we write the earlier code so this step is not needed (that is, so we get the result shown above).

Thank you mtf
I was able to clean up the code.

1 Like

thanks so much for this reply it help me out

1 Like

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

always i got 0 please anyone can help me solve this issue thanks, i’m begin confused !

daily_sales_replaced = daily_sales.replace(’;,;’, ‘**’)

daily_transactions = daily_sales_replaced.split(’,’)

daily_transactions_split =

for transaction in daily_transactions:
daily_transactions_split.append(transaction.split(’**’))

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

#print(transactions_clean)

customers =
sales =
thread_sold =

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

#print(customers)

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

#print(sales)
for thread in transactions_clean:
if thread[2] in thread:
thread = thread[2]
thread_sold.append(thread)

#print(thread_sold)
total_sales = 0

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

#print(total_sales)

thread_sold_split =
for thread in thread_sold:
thread_sold_split.append(thread.split(’&’))

#print(thread_sold_split)

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

what’s wrong with this code please, I’m begin confused for hours!

Perhaps when you joined the forum you skipped over the part about how to make your code look like code when it is posted? Guess now is a good time to find out that we don’t hold hands, here. Get with the program or expect hand outs which may not be as fitting as one would hope for.

can someone explain please why this works? what does [::] mean ?
Struggled with this problem for two days and I cant seem to get my head around it

Using list[::1] is a way of slicing a list in python that may not have been introduced in the lessons so far. Unless it has been covered in lessons it is probably wise to attempt to complete the problem without it so as to practice the skills you’ve covered to that point.

As for the slicing, if you’re curious it works as [start:end:step]. Defaults for these are effectively [0:-1:1] so when including the colons without a value you are using the default.

So for a list of 1:20 (inclusive) where we wanted every thrid value (starting at index 0)-

the_list = list(range(1, 21))
every_third_value = the_list[::3]
print(every_third_value)
output: [1, 4, 7, 10, 13, 16, 19]

For the same list if we wanted every thrid value starting at the 7th index but finishing before the 17th index-

the_list = list(range(1, 21))
every_third_value = the_list[7:16:3]
print(every_third_value)
output: [8, 11, 14]

Thank you!!! I’ve been in the same problem tooooooooo.

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

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

total_sales = 0

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

print(sales)

I am typing this code into the program which more or less the same as all of the other lines on here but when I print ‘total_sales’ I get a weird number (1498.7400000000005)

Anyone know why?
Thanks

That is because computers work with binary (floating point arithmetic). You can always round to 2 decimal places.

Thanks!

1 Like

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(’;,;’,’<->’)

#print(daily_sales_replaced)

daily_transactions = daily_sales_replaced.split(’,’)

#print(daily_transactions)

daily_transactions_split =

for daily in daily_transactions:

daily_transactions_split.append(daily.split(’<->’))

#print(daily_transactions_split)

transactions_clean =

for lst in daily_transactions_split:

transaction_clean =

for data in lst:

transaction_clean.append(data.replace('\n',"").strip(' '))

transactions_clean.append(transaction_clean)

#print(transactions_clean)

customers =

sales =

thread_sold =

for trans in transactions_clean:

customers.append(trans[0])

sales.append(trans[1])

thread_sold.append(trans[2])

#print(customers,’\n’,sales,’\n’,thread_sold,’\n’)

total_sales = 0

for sale in sales:

total_sales+=float(sale.strip(’$’))

#print(total_sales)

#print(thread_sold)

thread_sold_split =

for i in thread_sold:

if i.find(’&’)==-1:

thread_sold_split.append(i)

else:

a=i.split('&')

for b in a:

  thread_sold_split.append(b)

#print(thread_sold_split)

#or

for i in thread_sold:

for color in i.split(’&’):

thread_sold_split.append(color)

#print(thread_sold_split)

def color_count(color):

counter = 0

for i in thread_sold_split :

if i==color:

  counter+=1

return counter

#print(color_count(‘white’))

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

for i in colors:

print(‘Thread Shed sold {} threads of {} thread today’.format(color_count(i),i))

Ok this doesn’t answer the question, but I’m having trouble with python and I’m keep on getting the wrong answers so can anyone help me.

Any ideas? U can say anything that you think will help me.

Thank you guys!!! I really appreciate it.

Hello @promethus17! Could you explain what problems you are having?