Steve
Steve

Reputation: 480

Python printing format with functions

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

Upvotes: 0

Views: 78

Answers (3)

Joe T. Boka
Joe T. Boka

Reputation: 6581

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

Upvotes: 1

Mahi
Mahi

Reputation: 21932

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

Upvotes: 2

Piotr Jaszkowski
Piotr Jaszkowski

Reputation: 1178

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.

Upvotes: 3

Related Questions