Thread Shed - Step 22

Thread Shed Challenge: https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

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:
  print(
    '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)):
  print(
    '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:

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

daily_transactions_split = []
for entry in daily_transactions:
  daily_transactions_split.append(entry.split('@'))

# 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:
    trans_clean.append(item.strip())
  transactions_clean.append(trans_clean)

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

#print(thread_sold)

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

#print(total_sales)

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.