Thread Shed project - Help!

I have created this code and get a wrong answer, please can someone help me UNDERSTAND why is my code wrong

thread_sold_split =
for sale in thread_sold:
for thread in sale.split(“&”):
thread_sold_split.append(thread)

print(thread_sold_split)
print(‘\n’)
def color_count(colour):
for colour in thread_sold_split:
count = thread_sold_split.count(colour)
return count

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

for colour in colors:
print(“Thread Shed sold {0} threads of {1} thread today.”
.format(color_count(colour), colour))

RESULT:
Thread Shed sold 28 threads of red thread today.
Thread Shed sold 28 threads of yellow thread today.
Thread Shed sold 28 threads of green thread today.
Thread Shed sold 28 threads of white thread today.
Thread Shed sold 28 threads of black thread today.
Thread Shed sold 28 threads of blue thread today.
Thread Shed sold 28 threads of purple thread today.

Correct answer would be different numbers, not 28 for all

Indentation is very important in Python. When you paste code directly into the forums, the indentation and formatting is lost.

To preserve code formatting in forum posts, see: How do I format code in my posts?

At the bottom of the project/exercise, there should be a “Copy to clipboard” button. After copying, you can then paste all of your code by using the </> button in the forum post editor as explained in the linked thread.

1 Like

Thank you for your reply, Forgive me, I am new. Below is the code using “Copy file” , I can’t seem to be able to edit in the main message.
At the point where you see thread_sold_split = I have a list that has some threads listed individually eg “White” and some threads listed in pairs “purple&green” so I’ve split them at “&” which worked fine, resulting in a list of individual threads.
Then I had to create a function that counts how many white threads I have. Which worked fine and the number is 28. However, the developer video used If statement to get the same result. I figured .count method have correct result for white, so it should work but it doesn’t work for other colours.
Now as a last task, I have to get it to print out number of threads for all the colours but it results in 28 for all, which is incorrect.
Hope I’m making sense, thank you so much for your time.

thread_sold_split = []  
for sale in thread_sold:
  for thread in sale.split("&"):
    thread_sold_split.append(thread)
   
print(thread_sold_split)
print('\n')
def color_count(colour):
  for colour in thread_sold_split:
    count = thread_sold_split.count(colour)
    return count

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

for colour in colors:
  print("Thread Shed sold {0} threads of {1} thread today."
  .format(color_count(colour), colour))
1 Like

having done further research I understand that my for loop immediately returns the count for the first colour, so it never continues to count the other colours. I still don’t know why, I may not understand the “for loops” that well.

The problem lies in the color_count function.

# You wrote:
def color_count(colour):
  for colour in thread_sold_split:
    count = thread_sold_split.count(colour)
    return count

There are a number of problems with the above snippet of code.

  • Firstly, the parameter of your function is colour. Whatever input is passed as the argument to the color_count function will be assigned to this parameter. That is fine and as intended.
    But, in the very next statement,
for colour in thread_sold_split:

you discard the argument. The above loop will iterate over all the elements of the thread_sold_split list and in each iteration, an element will be assigned to the loop variable colour. You should choose a different name for the loop variable. You shouldn’t use the same variable name for both the loop variable and the parameter. Consider the following trivial example,

# In this version, the argument is overwritten since the 
# loop variable n has the same name as the parameter n.
def func(n):
    for n in [1, 2, 3]:
        print(n)
    print(n)
    
func(10)
# 1
# 2
# 3
# 3

# Compare with this version which uses i as the loop variable and
# hence avoids corrupting the value assigned to the parameter n.
def func(n):
    for i in [1, 2, 3]:
        print(i)
    print(n)
    
func(10)
# 1
# 2
# 3
# 10
  • Secondly, your function will only complete one iteration and then exit immediately because of the indentation of the return statement. Consider the following example,
# In this version, the return statement is indented as part of the 
# loop body. As soon as the return statement is executed, we 
# exit the function. Only the first iteration is completed. 
def f():
    total = 0
    for i in [1, 2, 3]:
        total += i
        return total
        
print(f()) 
# 1

# In this version, the return statement is not part of the loop body.
# Notice the indentation. The return statement is indented as the
# body of the function, but not indented as part of loop body.
# The loop will complete all iterations and then the return 
# will be executed.
def f():
    total = 0
    for i in [1, 2, 3]:
        total += i
    return total
        
print(f()) 
# 6
  • Thirdly, even if you fix the indentation, your code will not work properly. Instead, consider the following:
def color_count(colour):
    count = thread_sold_split.count(colour)
    return count

Since you have decided to use the count method, so the loop is unnecessary. The count method will automatically iterate over the thread_sold_split list and keep a running total of all elements that match the colour provided as the argument. You can then simply return the count. This should give you the correct results.

If you didn’t want to use the count method, then you could write an explicit loop and use an if statement to increment the total whenever a match is found. Once the loop finishes, you can return the total. But with the count method, the looping is implicitly handled by the method.

If something is unclear, share your thoughts on what you find confusing.

1 Like

That is so helpful, thank you so much for such a thorough explanation. You have really helped me understand quite few things better, I feel like I had and prob still have gaps that will only diminish with practice.
The code works now, but more importantly, has given me better understanding of .count, loops and variables.
Thank you so much, have a lovely evening!

1 Like