0

Trying to build a recursive def that will counts how many times the number '7' occurs in a given number. I dont understand why my return keeps giving me None.

def count7(N):
    '''
    N: a non-negative integer
    '''
    x = 0
    def helpcount7(N,x):
        Ld = N % 10
        NewN = N // 10
        if Ld == 7:
            x +=1
        if NewN != 0:
            helpcount7(NewN,x)
        else:
            print(x)
    return helpcount7(N,x)


print(count7(717))

for example, if I input 717 my answer would be:

2 None

Ubadah
  • 7
  • 3

1 Answers1

0

The reason is because you are not returning anything from your helpcount7() function. You should return something from it, for example:

def count7(N):
    '''
    N: a non-negative integer
    '''
    x = 0
    def helpcount7(N,x):
        Ld = N % 10
        NewN = N // 10
        if Ld == 7:
            x +=1
        if NewN != 0:
            return helpcount7(NewN,x)
        else:
            return x
    return helpcount7(N,x)


print(count7(717))
errata
  • 5,695
  • 10
  • 54
  • 99