1

I'm having this extremely strange problem and I guess it's something simple but it just makes no sense to me.

CodeWars question states:

Define a function that takes a multi digit number and multiplies each digit until you get a single digit answer, so for example persistence(39) would compute 3*9 = 27, 2*7 = 14, 1*4 = 4, return 4

I wanted to try and solve this recursively, and I have the answer, but the program will never break out of the function. I've stepped through this program multiple times and it gets to the return statement and then jumps back up into the if statement and calls persistence again.

Code below:

def persistence(n):


    arg_to_list = list(str(n))

    # If the argument is > one digit
    if len(arg_to_list) > 1:
        total = int(arg_to_list[0])
        for digit in arg_to_list[1:]:
            # Don't want any muplication by 0
            if digit != "0":
                total *= int(digit)

        # Call the function again on the total
        persistence(total)
    else:
        return n
halfer
  • 19,824
  • 17
  • 99
  • 186
Aaron Nolan
  • 15
  • 1
  • 4
  • 1
    for what input it's not stopping and you should add return statement before function call `return persistence(total)` – deadshot May 16 '20 at 17:20
  • Any input, i tried the number 39 like in the question, the function works out the correct answer which is 4, and when the debugger hits the return statement, it just jumps up two lines of code back to `persistence(total)` – Aaron Nolan May 16 '20 at 17:22
  • I just ran the code and I'm not getting any infinite loops, and it exits fine. Does the function end when you test it independently? Additionally, are you struggling to get an output that isn't None? – Kelo May 16 '20 at 17:23
  • 3
    I've also testded the same code and it works fine. However, you have to replace `persistance(total)` by `return persistance(total)` if you want your function to output the result. – Tristan Nemoz May 16 '20 at 17:25
  • 1
    That's expected. You use recursion, remember? But you're missing the return in the line where you call `persistence`: `return persistence(total)` – Matthias May 16 '20 at 17:25
  • @Aaron it's working without any problem – deadshot May 16 '20 at 17:25
  • `return persistence(total)` solves it. – whitespace May 16 '20 at 17:26
  • You're simply missing a return in front of persistence(total): return persistence(total) – Sylvaus May 16 '20 at 17:28
  • Yeah the return statement fixed it, thank you all for the help. My thinking was that I only needed a return statement when I expected the program to return the answer, which was after I got down to only one digit. My confusion was then not helped as when i started searching for a solution, this is the first result I got on google which does not use return statements https://realpython.com/python-thinking-recursively/, but I see now the first example doesn't use any return statements. Thanks again for the info! – Aaron Nolan May 16 '20 at 17:38

1 Answers1

2

You are not returning anything unless the number is 1-digit already, so your recursion doesn't pass over the result up. Here is what your code should look like (note the return statement just before else: - it is the only change from the original code):

def persistence(n):


    arg_to_list = list(str(n))

    # If the argument is > one digit
    if len(arg_to_list) > 1:
        total = int(arg_to_list[0])
        for digit in arg_to_list[1:]:
            # Don't want any muplication by 0
            if digit != "0":
                total *= int(digit)

        # Call the function again on the total
        return persistence(total)
    else:
        return n
Victor
  • 418
  • 2
  • 10
  • 1
    Yeah the return statement fixed it, thank you all for the help. My thinking was that I only needed a return statement when I expected the program to return the answer, which was after I got down to only one digit. My confusion was then not helped as when i started searching for a solution, this is the first result I got on google which does not use return statements https://realpython.com/python-thinking-recursively, but I see now the first example doesn't use any return statements. Thanks again for the info! – Aaron Nolan May 16 '20 at 17:39
  • Please find my answer below: def persistence(n): b = [] total = 0 for i in str(n): b.append(int(i)) for i in range(len(b)-1): total = b[i] *b[i+1] if int(total) >= 9 : total = str(total) n = total persistence(n) if 0 <= int(total) <= 9: print (total) persistence(39) My code is now working . however earlier it was going into loop as i was not putting the "if 0 <= int(total) <= 9" condition . Hope it helps – Diwakar SHARMA May 16 '20 at 18:18