1

Have an unusual request regarding code implementation at ideone:

n = 42
new= []
m = 0

def convert(n,m):
    print "round #:", m,"n :", n
    a = ((n+1) % 3)-1
    if n:
        new.append(a); print 'new :', new
        print 'call convert(' (n+1)//3,',',m+1,'):',convert((n+1)//3,m+1)
        print 'after convert(' , n, ',' ,m,  ')','\n'
    else:
        print 'n==',n, 'new: ::: ', new,'\n\n',
        return new, n

print  convert(n,m), "<<---- None returned ? "

I don't know why 'None' is printed in the output below. The output 'None' is printed in two places (cases):

Case #1. print 'after convert(' , n, ',' ,m, ')','\n'

Case #2. print convert(n,m), "<<---- None returned ? "

I am not at all clear why if the default case of 'n=0' has proper value of list 'new', then how is it that in case #2, 'None' is returned.

According to me, 'new' contents should be printed in case #2.


Even more confusing is: why case #1 is returning 'None' each time for non-zero value of 'n'?

According to me, there should be no 'None' printed for case #1.

enter image description here

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
jiten
  • 193
  • 2
  • 10
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – VLAZ Feb 13 '21 at 08:56

1 Answers1

1

If you want to avoid None from being your output, we have to ensure that your function returns a value.

In your case, there is a return statement in the else statement but not the if statement.

You might either introduce a return statement for the if part or change the indentation for the function return statement.

n = 42
new= []
m = 0

def convert(n,m):
    print "round #:", m,"n :", n
    a = ((n+1) % 3)-1
    if n:
        new.append(a); print 'new :', new
        print 'call convert(' +  str((n+1)//3,) + ',',m+1,'):',convert((n+1)//3,m+1)
        print 'after convert(' , n, ',' ,m,  ')','\n'
    else:
        print 'n==',n, 'new: ::: ', new,'\n\n',
    return new, n

print  convert(n,m)
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
  • I think you are stating for Case #2, i.e. for printing contents of list 'new'. But the unasked for 'None' in case #1 is confusing a lot. – jiten Feb 13 '21 at 04:26
  • 1
    Each time you call convert without a return, you get a None. – Siong Thye Goh Feb 13 '21 at 04:35
  • 1
    btw, you are encouraged to change to Python 3. – Siong Thye Goh Feb 13 '21 at 05:01
  • I am good at understanding and coding basic python code. But, got via a mse post (comment by : djangofan to https://math.stackexchange.com/q/695979/424260) code in java at: https://repl.it/@djangofan/RecusiveTaskFibonaccijava. Request to know if forking is needed in it or not. If yes, why a normal (sequential) code wouldn't work. – jiten Feb 17 '21 at 07:32
  • what are you trying to do? the author seems to want a recursive implementation. – Siong Thye Goh Feb 17 '21 at 08:49
  • Thanks. If just recursive implementation is needed, then why fork is used on line #25, and not just sequential implementation? – jiten Feb 17 '21 at 14:57
  • Please elaborate on my last comment. – jiten Feb 18 '21 at 01:37
  • [here](https://en.wikipedia.org/wiki/Fork–join_model) is more reading on fork-join model. mainly parallel computing. – Siong Thye Goh Feb 18 '21 at 02:34
  • Request what the code is doing (by forking out each time), or even better (sorry, but couldn't help) have it in python. Also, is it needed? For Fibonacci series computation, answer at: https://softwareengineering.stackexchange.com/a/238733/327853 , states it is a bad choice. – jiten Feb 18 '21 at 02:54
  • if you have a new specific question, ask it as a post, avoid long comment threads. – Siong Thye Goh Feb 18 '21 at 04:09
  • Lack courage, had it been mse would had. If agree, can open chat. You can help otherwise by making a new post. – jiten Feb 18 '21 at 07:00