0
# Compute the sum of every single digits of variable.
def transform(variable):
    if variable < 10:
        return variable
    else:
        return variable % 10 + transform(variable // 10)

# Count the number of times executing transform method
# until final result of transform method is single digit.
# Then, return final result of transform method and the number of times.
def f(variable, c = 0):
    if variable < 10 and c == 0:
        return variable, c
    else:
        variable = transform(variable)
        c += 1
        if variable < 10:
            print(variable, c) # Before return, i'd like to check these output.
            return variable, c
        else:
            f(variable, c)

if __name__ == "__main__":
    x = int(input())
    x = f(x)
    print(x)

Through 'print(variable, c)', I checked the function's output and expected that I get variable, c which are both integers. But, when i input integers like 1234567, i get None, the function's output even 1, 3 is printed as variable, c. I don't understand this mismatch.

JAEWON LEE
  • 11
  • 3
  • You ignore the return value of the recursive call; recursive calls don't automatically force the caller to return with the same value. – chepner Dec 20 '22 at 13:32

0 Answers0