为什么这个Python函数只有一个输出? [英] Why does this Python function only have one output?
本文介绍了为什么这个Python函数只有一个输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个非常简单的Python函数,但有一部分我很困惑。该函数被称为更大,它需要两个数字作为输入并输出更大的数字。 (我只能使用if语句,没有elses)
I have this very simple Python function, but there is one part I am confused about. The function is called bigger and it takes two numbers as inputs and outputs the bigger number. (I could only use if statements, no elses)
这是代码:
def bigger(x, y):
if x > y:
return x
return y
我认为此代码会返回 y
如果 y
更大(它确实如此),但它会返回 x
和 y
如果 x
更大(它只返回 x
)。为什么它只返回一个输出? Python函数只能有一个输出吗?
I would think that this code would return y
if y
is bigger (which it does), but it would return x
and y
if x
is bigger (it only returns x
). Why does it only return one output? Can Python functions only have one output?
推荐答案
def bigger(x, y):
if x > y:
# when x>y, x will be returned and 'return y' in the last line will not be executed.
return x
# only if x<y, this line will be executed.
return y
这篇关于为什么这个Python函数只有一个输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文