Thread Shed step 19

I have a question i did this step like this:

def color_count(color): return thread_sold_split.count(color)

but in the video the instructor did it like this:

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

my question is what is the difference between them, because the both of them print the same

Thanks

So, your code loops through all the colors and adds them to a running total and returns that total for each color?
It would be helpful to see all of the code. (Both codebytes above output nothing)

I think they’re avoiding having people memorize a bunch of prebuilt methods while they’re teaching how the code works. A math teacher will make you work out the proofs of a formula to teach you why it works and once you’re done you just use the formula.

They do often say that there are many ways to get the job done, if they don’t ask you to do it a specific way it’s fine to walk your own path.

To add to what’s been stated already, it’s a bit silly to write a function that simply returns the result of calling a built-in function. Presumably you are using the color_count function to assign a variable to a value representing the number of spools (or whatever) of the color argument that were sold, so something like:

spools_of_blue = color_count('blue')

If you are going to use a built-in function rather than writing your own (your choice), it would make more sense to simply do:

spools_of_blue = thread_sold_split.count('blue')
1 Like

thanks for all answers, i’m new learning phyton, so i didn’t see that :sweat_smile:

1 Like