Python code not printing output

Python code not printing output
0

#1
# your code goes here
def calculate(m,n):
    result = m * n 
    return result 
    print result
calculate(2,3)

This is python code and there is proper indentation in it ,as have tried to put it here also but editor is not supporting .Any way it is running successfully over my editor but it is not printing anything?Any help


#2

I don’t write python … but it sure looks like you have a return statement prior to print.

If python is like every other language I have ever heard of, that return statement will termination your function right there and, well, return.

Move that down two lines and try again?

Edit: Nevermind, I’m assuming that the structure is such you are calling calculate after it’s been defined.

In that case try,

print calculate(2,3)

instead of

print result

Calculate will return a value and print will print it … at least that’s what I would expect.

But, like I said, I’m not at all familiar with Python.


#3

Thanks for replying it works out.Its general question not specific to python
Tell me one thing that if we use return before print how it is actually influencing code.


#4

Inside a function, the return keyword tells the function, “I am done - return to the code/function you were executing before you started executing me.” This effectively terminates the function wherever the return statement is. Nothing else in the function will execute after that statement.

Additionally, the return statement can specify a value to pass back to the calling function. In the case with your code, the value being returned was whatever was stored in the result variable.

I can’t tell because of the lack of indentation in the forum post, but the issue is that you were trying to print result from outside the function or you were trying to print result inside the function but after the return statement, so that line would never run.

Instead, you need to print from within the function, prior to return. Or print the result returned from the function, which is what I suggested.

Hope that helps!


#5

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.