Percentage of digits in n that are even Question 7 from Lab 3 of Berkeley's CS61A course says:

The following examples of recursive functions show some examples of common recursion mistakes. Fix them so that they work as intended. [...]
def even_digits(n):
    &quot;&quot;&quot;Return the percentage of digits in n that are even.

    &gt;&gt;&gt; even_digits(23479837) # 3 / 8
    0.375
    &quot;&quot;&quot;
    if n == 0:
        return num_digits / num_evens
    num_digits, num_evens = 0, 0
    if n % 2 == 0:
        counter += 1
    num_evens += 1
    return (even_digits(n // 10) + num_evens) / counter


As per the assignment, below code is suppose to solve this problem, recursively (MUST not be non-recursive).
def even_digits(n, count_even = 0, count = 0):
    &quot;&quot;&quot;Return the percentage of digits in n that are even.

    &gt;&gt;&gt; even_digits(23479837) # 3 / 8
    0.375
    &quot;&quot;&quot;
    if n == 0:
        return count_even / count
    if (n % 2) == 0:
        return even_digits(n // 10, count_even + 1, count + 1)
    else:
        return even_digits(n // 10, count_even, count + 1)

Is this code readable? Can we improve this solution?
There is a serious error in your code. If the function is called as even_digits(0), then it causes the following error:

ZeroDivisionError: integer division or modulo by zero


since in the first if you divide by count which is 0.

So, before improving the code, you should check if it is correct.

ADDED:

THE CASE OF n = 0

It is interesting to understand what should be the result in case the input (that is n) is 0. 

The problem is labeled as: 


  Percentage of digits in n that are even


and the natural interpretation is:


  Percentage of digits in the decimal representation of n that are even


So, what result should give the function when provided with the value 0? I argue it should be 1, since every decimal representation of 0 has only zeroes as digits, so it has a 100% even digits.