Thread Shed - Color_Count

Hello again! I’ve stumbled into another problem with Thread Shed! Here’s the link to the exercise:

Thread Shed

My code is returning zero when it should be returning numbers in their 20s. I went through the walk through video and even though my code was similar I changed it to be exactly the same! Yet, it still doesn’t work! Here’s my code:


def color_count(color):
  color_total = 0
  for thread_color in thread_sold_split:
    if color == thread_color:
      color_total += 1
  return color_total

print(color_count('white'))

Your help will be much very much appreciated!

Print the values you’re iterating through. This should give you big clues as to why it’s not matching up.

1 Like

Is thread_sold_split correct? Can you check with print() to see it contains 'white'.

Yes, I have checked with print() and it contains all the white.

I printed thread_sold_split and all the colors that are meant to be there are there. Is printing thread_sold_split what you meant?

Using your code, I don’t see any issue

def color_count(color):
    color_total = 0
    for thread_color in thread_sold_split:
        if color == thread_color:
            color_total += 1
    return color_total

print(color_count('white'))
print(thread_sold_split.count('white'))

Output:
28
28

Hence, unless thread_sold_split is messed up? :thinking:
You’re almost there!

1 Like

Yes, it is an issue with thread sold split. I didn’t add each color individually to the list. I’ve sorted that part but it’s still not printing what is to be expected! I’m asking my tutor for help so will update this when I figure it out!

It’s fixed:


thread_sold_split = []

for thread in thread_sold:

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

    thread_sold_split.append(color)

#print(thread_sold_split)

  

def color_count(color):

  color_total = 0

  for thread_color in thread_sold_split:

    if color == thread_color:

      color_total += 1

    return color_total

print(color_count('white'))

The return needed to be moved back and now it works! Phew! Thanks so much for your help, all!

1 Like

Glad you found it!

The word iterating is used to refer to loops. A for-loop cycles (or iterates, or loops) through a list of numbers.

Often times when debugging it’s a useful thing to do.

2 Likes