Strings: Project Thread Shed

So I have done the assignment thankfully. But for the last task I think It could be better.


Now, using the list colors , the string method .format() , and the function color_count , iterate through thread_sold_split and print a sentence that says how many threads of each color were sold today.


My code:

colors = ['red','yellow','green','white','black','blue','purple']
def color_count(color):
  count_red = 0
  count_yellow = 0
  count_green = 0
  count_white = 0
  count_black = 0
  count_blue = 0
  count_purple = 0
  for i in range(len(thread_sold_split)):
    for m in range(len(thread_sold_split[i])):
      for x in range(len(color)):
        if thread_sold_split[i][m] == color[x]:
          if color[x] == 'red':  
            count_red += 1
          elif color[x] == 'yellow:':
            count_yellow += 1
          elif color[x] == 'green':
            count_green += 1
          elif color[x] == 'white':
            count_white += 1
          elif color[x] == 'black':
            count_black += 1
          elif color[x] == 'blue':
            count_blue += 1
          elif color[x] == 'purple':
            count_purple += 1
    
  return "Thread Shed sold {} threads of {} thread today.".format(count_red, color[0]), "Thread Shed sold {} threads of {} thread today.".format(count_yellow, color[1]), "Thread Shed sold {} threads of {} thread today.".format(count_green, color[2]), "Thread Shed sold {} threads of {} thread today.".format(count_white, color[3]), "Thread Shed sold {} threads of {} thread today.".format(count_black, color[4]), "Thread Shed sold {} threads of {} thread today.".format(count_blue, color[5]), "Thread Shed sold {} threads of {} thread today.".format(count_purple, color[6])

I think that return thing can be made better. It turns out like this

('Thread sold 24 threads of  thread today.', 'Thread sold 0 threads of thread today.' ... ')

Instead of

Thread sold 24 threads of red thread today.
Thread sold 0 threads of yellow thread today.
...

Could you tell me how to do it better (at least a way to not make me repeat the code for several times)? Please do not use any methods, or anything, that are not covered in CodeCademy if you can. Very much appreciated.

I believe the instructions for that task ask for a function which takes one argument, the colour, and returns the count for that specific colour.

Therefore you don’t need a single function to count each and every individual colour. You just call that single function whilst iterating through your list of colours in order to get the counts for each individual colour contained within that list. If you follow the instructions step by step it should naturally lead you to that solution.

If you’re interested in an alternative method, dictionaries and the Counter class would probably be the way to go but I’d avoid using them until they’re introduced in the lessons as you may trivialise tasks where you could still be learning important concepts. Where possible stick close to the instructions as many higher-level functions and classes aren’t introduced so as to get you to practice with the basic syntax of the language itself.

I see. Thank you for taking your time answering my question.