0

I am currently learning Python and have written this basic function. However the output is on several lines and does not show the answer after the "Here is some math:". What is wrong?

Thank you

def ink(a, b):
    print "Here is some math:"
    return a + b        
add = ink(1, 59)    
fad = ink(2, 9)    
bad = ink(4, 2)     
print add    
print fad    
print bad

Output :

Here is some math:
Here is some math:
Here is some math:
60
11
6

EDIT: Why is it not printing

Output :

Here is some math:
60
Here is some math:
11
Here is some math:
6
7
  • Did you expect return a+ b to have printed the result? Or did you expect the execution of the function would be delayed untill - print add ? Commented Sep 10, 2015 at 9:33
  • No I expected the print add, print fad etc to print it, but why does it not display as Here is some math: 60 Here is some math: 11 ?? Commented Sep 10, 2015 at 9:34
  • It is printing correctly, I do not understand what you are expecting from that code. Why would you expect that to happen? You are only printing the result of addition after calling all the functions. Commented Sep 10, 2015 at 9:34
  • I have added an edit sorry it was not clear before.. Commented Sep 10, 2015 at 9:37
  • What's scenario behind this? I think also in other languages as Java or C++, print will be executed in that order~ Commented Sep 10, 2015 at 9:51

3 Answers 3

3

The function ink is printing Here is some math: when it's called, when its return value is assigned in

add = ink(1, 59)

And the result value is printed in

print add

To achieve what you want you would have to do

print ink(1, 59)

EDIT: Even better, if it's for debugging:

def ink(a, b):
    result = a + b
    print "Here is some math:"
    print result
    return result

Anyway, I believe that what you wrote here is only an example. You should not print anything from functions that calculate something if it's not for debugging purposes. If it is for debugging, then the entire message should be contained in the body of a function and not be split like that.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok thanks, so this couldnt be achieved by printing the variables? (Sorry i am new so trying to understand :) )
I'm not aware of a clean way to do it, that doesn't mean it doesn't exist.
Your answer print ink(1, 59) only prints the number, doesn't print the "Here is some math:"
@JoeR I believe it prints it as the function ink is called.
Thank you, I forgot that by calling the function it executes the print.. I understand now :) cheers
|
2

Whenever you call a function, its body gets executed immediately. So when you call add = ink(1, 59), the ink function's body, which contains a print statement, is executed. Thus it prints out "Here is some math:".

Once the function's body reaches a return statement, the function's execution will end and the return statement returns a value to the place where the function was called. So when you you do:

add = ink(1, 59)

The result is returned by ink(1, 59), then stored to add, but the result doesn't get printed yet.

You then repeat the same for other variables (fad and bad), which is why you get the printed "Here is some math:" three times before seeing any numbers. Only later you print the actual results using:

print add
print fad
print bad

What you should do instead, is to have functions only calculate the results:

def ink(a, b):
    return a + b

And usually you'd want to do the prints and inputs outside of the functions (or in main function):

add = ink(1, 59)
fad = ink(2, 9)
bad = ink(4, 2)

print "Here's some math:", add
print "Here's some math:", fad
print "Here's some math:", bad

Although repeated code is often considered bad, so you could use a for loop here (you should study more about for loops if you don't know how they work):

for result in (add, fad, bad):
    print "Here's some math:", result

3 Comments

Ah brilliant, I understand that now :) Thank you!
@NZSteve Good to know it helped! I've updated the answer with a "proper" solution, if you're interested :)
Loops are a bit further ahead of me, but your explanation makes sense thanks!
1

You have to return what you want to print:

def ink(a, b):
    return "Here is some math: {}".format(a + b)
add = ink(1, 59)
fad = ink(2, 9)
bad = ink(4, 2) 

print add
print fad
print bad

Output:

Here is some math: 60
Here is some math: 11
Here is some math: 6

1 Comment

Thank you! I see that will also work so will play with that too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.