繁体   English   中英

找到最多3个变量的函数不会返回任何内容

[英]Function to find maximum of 3 variables isn't returning anything

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

def biggest(a,y,z):
    Max=a
    if y>Max:
        Max=y
        if z>Max:
            Max=z
            return Max
>>> max(2, 3, 5)
5
>>> help(max)

内置模块内置函数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) 

这是因为你的功能有缩进。 你已经将指令return Max放在if链的最内层,所以只有当最大值是z数时才返回结果。 ay最大时,它什么都不返回。 你可以在这里阅读更多关于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

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

我会建议这个......

def max_of_three(x,y,z):
    Max = x
    if y > Max:
        Max = y
    if z > Max:
        Max =z
    print Max

max_of_three(3,4,2)

打印4

在python中使用“max”函数会更好,关于max的最好的事情是,它不限于3或4个参数。

 """
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value

    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.
    """

max_value = max(1, 2, 3)
print(max_value) # will return 3
x = float(input("Enter the first number: "))
y = float(input("Enter the second number: "))
z = float(input("Enter the third number: "))
if x >= y and x >= z:
    maximum = x
elif y >= z and y >= x:
    maximum = y
else:
    maximum = z
print("The maximum no. b/w : ", x, ",", y, "and", z, "is", maximum)
  def findMax(a, b,c)

     return max(a, b,c)

  print(findMax(6, 9, -5)

我会把它粘贴在这里以防一些新的python学生来到这里。

def biggest (a, b, c):
    imax = a
    if (b > imax) and (b > c):
            imax = b
    elif (c > imax):
            imax = c
    return imax

#to test
print (biggest(5,6,7))
if x>y>z:
    print ("max number is x : ",x)
if z>y : 
    print ("max number is z : ",z)
if y>z :
    print  ("max number is y : ",y)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM