在python中找到最多三个数字

ali*_*web 2 python function

我的问题是什么?我跑,biggest(10,5,6)但没有回报.

def biggest(a,y,z):
    Max=a
    if y>Max:
        Max=y
        if z>Max:
            Max=z
            return Max
Run Code Online (Sandbox Code Playgroud)

Min*_*gyu 30

>>> max(2, 3, 5)
5
>>> help(max)
Run Code Online (Sandbox Code Playgroud)

内置模块内置函数max的帮助:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value

    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.
(END) 
Run Code Online (Sandbox Code Playgroud)


aga*_*aga 8

这是因为你的函数缩进了.你已经把指令return Max放在了你的链的最内层if,所以只有当最大值是z数字时才会返回结果.当a或者y是最大值时它什么都不返回.你可以在这里阅读更多关于python对缩进的态度.

def biggest(a, y, z):
    Max = a
    if y > Max:
        Max = y    
    if z > Max:
        Max = z
        if y > z:
            Max = y
    return Max
Run Code Online (Sandbox Code Playgroud)

如果您不需要实现自己的功能,可以使用内置max函数,正如Mingyu 指出的那样.