dimanche 23 août 2020

Counting zeroes using Recursion and if-else in python

def countz(n):
    if n<10:
        if n==0:
            return 1
        else:
            return 0
    small=countz(n//10)
    if n%10==0:
        return small+1
    else:
        return small


from sys import setrecursionlimit
setrecursionlimit(11000)
n = int(input())
print(countz(n))

Someone helped me write this code, I did not understand why he used the condition n<10 in the base case of the recursion. I though the code would work without that condition but it didn't. Can anyone help me understand why the code is not working without that condition/ what is the real purpose or reason for that condition?

Aucun commentaire:

Enregistrer un commentaire