[英]Strange print output for Python function return
I'm a newbie who just starting out. 我是刚开始的新手。 I have been playing around with function and I can't understand why I get the output below from the code below: 我一直在玩功能,但我不明白为什么我从下面的代码中得到下面的输出:
Why doesn't it print the return value as well as text from a function in continuous lines, why does the output look like it is looping through the functions twice? 它为什么不连续打印行中的返回值和文本,为什么输出看起来像遍历函数两次?
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
plus = add(1,1)
minus = subtract(1,1)
times = multiply(1,1)
print plus
print minus
print times
The output I get is: 我得到的输出是:
ADDING 1 + 1
SUBTRACTING 1 - 1
MULTIPLYING 1 * 1
2
0
1
Your code is written this way. 您的代码是以这种方式编写的。 First you execute all three functions. 首先,您执行所有三个功能。 Then you print the results of all three functions. 然后,您将打印所有三个功能的结果。
plus = add(1,1) # ADDING 1 + 1
minus = subtract(1,1) # SUBTRACTING 1 - 1
times = multiply(1,1) # MULTIPLYING 1 * 1
print plus # 2
print minus # 0
print times # 1
If you want the results interleaved with the calculations, then interleave them. 如果您希望结果与计算交错,则对它们进行交错。
plus = add(1,1) # ADDING 1 + 1
print plus # 2
minus = subtract(1,1) # SUBTRACTING 1 - 1
print minus # 0
times = multiply(1,1) # MULTIPLYING 1 * 1
print times # 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.