Skip to content
Advertisement

counting odd numbers in a list python

This is a part of my homework assignment and im close to the final answer but not quite yet. I need to write a function that counts odd numbers in a list.

Create a recursive function count_odd(l) which takes as its only argument a list of integers. The function will return a count of the number of list elements that are odd, i.e., not evenly divisible by 2.

JavaScript

Here is what i have so far: #- recursive function count_odd -#

JavaScript

Can u help explain what im missing. The first two test harness works fine but i cant get the final two. Thanks!

Advertisement

Answer

Are slices ok? Doesn’t feel recursive to me, but I guess the whole thing is kind of against usual idioms (i.e. – recursion of this sort in Python):

JavaScript

x%2 is 1 for odds, 0 for evens. If you are uncomfortable with it or just don’t understand it, use the following in place of the last line above:

JavaScript

PS – this is pretty recursive, see what your teacher says if you turn this in =P

JavaScript
Advertisement