Thread shed 2

Hi! I am stuck on thread shed again.
Capture Capture
How can I check multiple colors? (Please watch the walkthrough video)
I hope @ajax5260537031 (Tezza Chen), who helped me last time, could see this topic.


Hello! Could you post a link to this exercise? What ideas/code for checking them do you already have? (On the forums, we’d rather just give hints over direct answers.)

1 Like

Hi! I forgot about .find(). Thank you for reminding me.

Also, instead of i.find("&") != -1, I just used i.find("&") == 1.

EDIT: Whoops, I must use i.find("&") != -1.

Please also help me with step 22.

What code have you written so far, or, how do you think you can achieve this step?

1 Like
daily_sales = \
"""Long String"""

# Start coding below!
daily_sales_replaced = daily_sales.replace(";,;", ";")
daily_transactions = daily_sales_replaced.split(",")
daily_transactions_split = [i.split(";") for i in daily_transactions]
transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for trans in transaction:
    transstrip = trans.strip()
customers = []
sales = []
thread_sold = []
for transaction in transactions_clean:
total_sales = 0
for sale in sales:
  salestrip = sale.strip("$")
  salefloat = float(salestrip)
  total_sales += salefloat
thread_sold_split = []
for color in thread_sold:
  if color.find("&") != -1:
    colorsplit = color.split("&")
    for csplit in colorsplit:

def color_count(color):
  count = 0
  for clr in thread_sold_split:
    if clr == color:
      count += 1
      count = count
  return count

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

If you use print() it helps you see what your code is doing.

@lisalisaj What should I print?


After you write each bit of code use a print() statement so you can see what the results of the code logic is. If it doesn’t look right or you get an error, then you can debug the code.

1 Like

I did it all, and nothing was wrong. My problem was step 22. How do I do it? (Without the project walkthrough video)

We don’t give away code answers on this forums as it violates the general rules of conduct here. How would you write the function to iterate through and give you a total number of how many of each color of threads sold that day?

What happens with your code here?

def color_count(color):
  count = 0
  for clr in thread_sold_split:
    if clr == color:
      count += 1
      count = count
  return count

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

Why is Tezza replying? LJ said not to give code answers.

Please see this message.

And also, what is set()

And also, what is set()

You can google “Python set” to learn what is set (it’s similar to to concept of “Set” in Maths). Meaning, it should not contain any duplicates. If you look at my code, I’ve assigned the result to a variable called distinct_color because I want to obtain a list of unique colors in thread_sold_split.
Again, like I said, that was my lazy way to get the result. Otherwise, you we to manually go thru’ thread_sold_split to find and create a list of unique colors. Sorry, I didn’t meant to confuse you, but as far coding is concerned, there’s no limit and boundary when you progress and learn more each time! :stuck_out_tongue_winking_eye:

Also, a set can have different order. For example, set("1234") has a ~4.1% chance of returning {"1", "2", "3", "4"}, a ~4.1% chance of returning {"1", "2", "4", "3"} , a ~4.1% chance of returning {"3", "1", "2", "4"}, and so on. The general formula for calculating the chance for something like the order of "12345" becoming the set is 1/n!.

In this case, I have 4 digits, so the calculation will be reduced to 1/4!, which becomes 1/24, which is 0.41667, or ~4.1%.

Ah yes, the community flagged this one. If you still want to see this one, click “View ignored content”.

Anyways, in step 22, I needed to use 2 different variables for the format function and the for loop. So I used “colour” (the British spelling) for the for loop and “color” (the American spelling) for the format method (why do I keep saying function). I used color in format, but to define color, I used colour. I just discovered that I used color as a reference in the format method. I changed that to color and it works. After 3 weeks on the forums, this thread, along with the original one, is now closed.

(By the way, I did not have to iterate over thread_sold_split!)