+ 1

Why becomes the result "None"

This is a function for an old style multiplication. The function works fine. It prints out the result in the last line of function. But then it returns "None"? What is going wrong? https://code.sololearn.com/cSbj96Hcnt8P/?ref=app

26th Nov 2019, 11:34 AM
Coding Cat
Coding Cat - avatar
4 ответов
+ 5
A helpful tool is an online visualization that display the programm steps with var contents and so on: http://www.pythontutor.com/visualize.html#mode=display
26th Nov 2019, 1:13 PM
Lothar
Lothar - avatar
+ 3
The reason is probably that your code can end and then goes back to function call. One point to end from multi() is your return statement in the else part , the other is at the end of the if part. And there nothing will be returned. As you are using a recursive call of multi(),it makes it a bit more complicated. BTW, there is an indentation issue with second if statement.
26th Nov 2019, 12:48 PM
Lothar
Lothar - avatar
+ 2
You need to return the recursive result otherwise the function simply ends after executing that statement, resulting in None being returned. I don't have proper explanation about this but #this fix for your code return multi(x, y, sum)
26th Nov 2019, 12:26 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Thank you both @Coding Panda and @Lothar. This was maybe a silly idea. I did it now with a while loop. This works as expected ;) What a nice tool. THX for this: http://www.pythontutor.com/visualize.html#mode=display
26th Nov 2019, 6:39 PM
Coding Cat
Coding Cat - avatar