Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return statement is not working in python inside if condition [duplicate]

def digital_root(n):
    s=0
    if  n < 10:
        return n
    else:
        while n>0:
            s+=n%10
            n=n//10
        digital_root(s)

I am having trouble submitting this question.

I made sure to return the digit and i checked that the digit was correct by outputting it to the logs, but the tests keep failing and saying that I am returning None. I am not.

like image 882
thewires2 Avatar asked Jul 20 '26 13:07

thewires2


1 Answers

You need a return before the recursive call:

def digital_root(n):
    s=0
    if  n < 10:
        return n
    else:
        while n>0:
            s+=n%10
            n=n//10
        return digital_root(s)        # added 'return' here
like image 100
Aziz Sonawalla Avatar answered Jul 23 '26 06:07

Aziz Sonawalla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!