4

While learning Python and browsing the internet I stumble upon a piece of code in w3schools.com. I tried to run it using their built-in site Python IDLE and using my own Python 3.9.0 Shell. What I got is two different outputs.

I want to know which output is the correct output and why is it providing two different outputs.

The Codes and Its Output

Built in site Python IDLE Built in site Python IDLE

Python 3.9.0 Shell Python 3.9.0 Shell

Notice that the number 21 is only printed(outputted) once when running the code using Built-in site Python IDLE, while it is printed(outputted) twice when running the code using Python 3.9.0 Shell.

My Own Debugging

I have tried a simple print statement debugging. Checking the result there is only one different, using Python 3.9.0 Shell the last return line is executed and it outputted the last result while using the Built-in site Python IDLE the last return is either not executed or not outputted, in this case, I believe it is the former, and I believe the correct output is the Python 3.9.0 Shell, but I have no idea why are there two different output.

Print Statement Using Python 3.9.0 Shell Result Part 1 Result Part 2

Print Statement Using Built-in site Python IDLE Result Part 1 Result Part 2

Source Code

def tri_recursion(k):
    if(k>0):
           result = k + tri_recursion(k-1)
           print(result)
    else:
           result = 0
    return result

tri_recursion(6)
Hai
  • 61
  • 6
  • 4
    A line like `tri_recursion(6)` will output the value of the expression in the interactive shell. It will have no such side effect when run in a script. Use `print(tri_recursion(6))` and both will have the same output. – user2390182 Oct 21 '20 at 06:37
  • 1
    When you have small code snippets, please add them in the question. When they are large you can link a gist from github or similar. Codes in images can't be copy pasted to reproduce your problem – Teshan Shanuka J Oct 21 '20 at 06:39
  • @schwobaseggl I think you got it correct. Please add it as an answer – Teshan Shanuka J Oct 21 '20 at 06:42
  • @schwobaseggl, thanks for the reply, I think you got it correct. – Hai Oct 21 '20 at 06:45
  • @TeshanShanukaJ, thanks for the reply, I had trouble using the small code snippets it kept telling me error in code formatting, I will try to edit it now. – Hai Oct 21 '20 at 06:47

3 Answers3

2

You have added a return result statement at the end. For any IDE, unless you print that value, it wouldn't be displayed. However, IDLE prints the return value as well. Technically, both outputs are correct, since both interpreters are configured to do different actions. As a small example,

def foo():
    return(1)

Running foo() on IDLE gives >>> 1, whereas it gives nothing on other IDE's, as there is no print statement

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
1

Both are correct.

The built-in python on the site is showing you the output of the execution of your program. This is equivalent to running the following program:

if __name__ == '__main__':
    tri_recursion(6)

add this at the end of your code, save it as test.py and run it like this:

python test.py

the results will be the same

The python shell is showing you the output of the REPL (Read-Eval-Print-Loop), the print statement will print to the screen, but the return value of the function is also printed, because of the REPL, there's no way to avoid this, it is so by design.

You could design your function to not return anything, but it wouldn't be recursive anymore.

fixmycode
  • 8,220
  • 2
  • 28
  • 43
1

both are correct you need to understand that the python shell is printing statement's output. when you write :

>>> tri_recursion(6)

it will execute first all the print inside function then it prints value that the last call returns.

pradeexsu
  • 1,029
  • 1
  • 10
  • 27