I’m working on the Thread Shed exercise in the Strings module (https://www.codecademy.com/courses/learn-python-3/projects/thread-shed) and have a question about step 19.
here is my simple solution:
def color_count(color):
return thread_sold_split.count(color)
but in the guided video solution they use this method:
def color_count(color):
color_total = 0
for thread_color in thread_sold_split:
if color == thread_color:
color_total += 1
return color_total
Is there a reason that using a loop is a better/more accurate solution here?
I may be wrong about this explanation and someone please correct me if I am.
I believe the reason why we use a loop statement here because we need the program to run through each string(color) to see how many times that color appears in the huge list that was provided; which you’ve already sorted out.
If you proceed with only your given code, the program will simply count every string(color) which you have sorted from the huge list.
To summarize my explanation, we use for because we want to know how many times a specific color(e.g white) appears, meanwhile your code implies that, we count ever single color in the list.
I may be wrong in my explanation, someone would deeper knowledge please correct me if I am.
Thanks for the Guidance!
did you figure it out, I’m new at it as well. I’m getting none when I print any color.
Could I please see a copy of your code.