Thread Shed - Step 22

Thread Shed Challenge:

Step 22 of the Thread Shed challenge has us printing out a string which includes the total of each color thread sold during the day.

My initial solution works, but since it uses an f-string, which has not actually been covered yet, I was curious as to how to do it using the methods covered so far in the course.

The video tutorial has this as the solution:

colors = ['red','yellow','green','white','black','blue','purple']
for color in colors:
    'Thread Shed sold {0} spools of {1} thread today.'
    .format(color_count(color), colors)

Trying this returns a string for each item with the correct number sold but with the entirety of the “colors” list. Looking at the code, it seems to be working as expected - it’s just calling the list rather than iterating through it.

So I tried this:

#for color in range(len(colors)):
    'Thread Shed sold {0} spools of {1} thread today.'
    .format(color_count(color), colors[color])

Now I have the name of each color printing as expected but the color_count function doesn’t seem to work - it returns 0 for each line. I tried troubleshooting this solution with no luck.

So my questions are:

  • What am I missing with the non-f-string solutions?
  • Should I be happy with the f-string solution even though it has yet to be introduced in the course? It does seem to be the most current method for such things.

Full version of my solution here:

daily_sales_replaced = daily_sales.replace(';,;', '@')
daily_transactions = daily_sales_replaced.split(',')

daily_transactions_split = []
for entry in daily_transactions:

# Needed a second "temp" variable in order to strip whitespace while keeping individual entries intact
transactions_clean = []
for entry in daily_transactions_split:
  trans_clean = []
  for item in entry:

customers = []
sales = []
thread_sold = []
for entry in transactions_clean:


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


thread_sold_split = []
for item in thread_sold:
  thread_sold_split += item.split('&')

def color_count(color):
  clr = 0
  for item in thread_sold_split:
    if item == color:
      clr += 1
  return clr
colors = ['red','yellow','green','white','black','blue','purple']
#for color in colors:
#  print(
#    'Thread Shed sold {0} spools of {1} thread today.'
#    .format(color_count(color), colors)
#    )

#for color in range(len(colors)):
#  print(
#    'Thread Shed sold {0} spools of {1} thread today.'
#    .format(color_count(color), colors[color])
#    )

for color in colors:
  print(f'Thread Shed sold {color_count(color)} of {color} thread today.')

I’m a little lost on what issue you have with the solution.
Edit: I can now see an issue.
The first versions seems perfectly reasonable but is that supposed to be color rather than colors? With colors it’d printing a full list every time which seems like a mistake.

Removing the for loop doesn’t make much sense and colors[color] would probably throw an error since it’s a list, not a dictionary.

A solution with f-strings is fine, they’re often very helpful and the syntax is very similar to the .format method but they are python3 so keep it in mind if you have work with python2 at any point.

If you wanted to convert your code to use .fomat the only change is in the print statement:

for color in colors:
    # named .format version
    print("Thread Shed sold {colorcount} of {color} thread today.".format(colorcount=color_count(color), color=color))
    # or the indexed version
    print("Thread Shed sold {0} of {1} thread today.".format(color_count(color), color))
1 Like

Consider what each value is before you use them. If you get a different result from what you expect then go look at the actions carried out, all of that is observable but you have to choose to look. You’re not getting 0 out of nowhere, you did a bunch of things which you can observe.

I used this as my solution to print out the number of thread sales by color but it is more involved that the clean solution offered in the tutorial.

Would this still be considered a valid/working solution?

# Function to count the number of times a color is sold
def color_count(color):
    count = 0
    for thread_color in thread_sold_split:
        for sub_thread_color in thread_color:
            if sub_thread_color == color:
                count += 1
        if color == thread_color:
            count += 1
    return count

# List of colors Thread Shed Offers for sale
colors = ['red','yellow','green','white','black','blue','purple']

print("The following numbers of thread colors were sold today:")
for i in range(len(colors)):
    print(str(color_count(colors[i])) + " {color} ".format(color=colors[i]) + "thread")

Prints out:

I noticed my function is not the same, but it gets me the correct amounts.

One thing I noticed was the last line, where it’s not necessary to use .format().

You can use this instead:

print(str(color_count(colors[i])) + colors[i])
1 Like