Thread Shed

This is for the Thread Shed project on strings.

I’m on the very last step of the project, and I’m writing the for loop to print out the statement, which seems to break after the first iteration, and I can’t seem to figure out why.

Here’s my code:

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

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

for color in colors:
  color_count = color_count(color)
  print('Thread Shed sold {color_count} {color} threads today.'.format(color_count=color_count, color=color))

It seems to work for the first iteration, but then throws a TypeError for the color_count variable inside the for loop saying that ‘int’ object is not callable.

for color in colors:
  color_count = color_count(color)

You pass a string assigned to the variable color as the argument to the function color_count
The function color_count returns an integer value.
You are then assigning this returned value to the variable color_count.
Now, color_count is no longer a function but an integer.
You shouldn’t assign the returned integer to a variable with the same name as the name of the function.

so assigning the integer to a variable with the same name as the function renders the function useless? As in, it can no longer be called because the computer no longer sees color_count as a function, but rather as just the integer from the first iteration? If that’s the case, then does it break because now that color_count is just an integer, the (color) argument just purely doesn’t make sense with the syntax?

1 Like

Your understanding is correct.


1 Like

Okay, thanks for your help!

1 Like