Python 查找最多 3 个变量的函数不返回任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18973575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function to find maximum of 3 variables isn't returning anything
提问by aliweb
What is my problem? I run biggest(10,5,6)
but it returns nothing.
我的问题是什么?我跑了,biggest(10,5,6)
但它什么也没返回。
def biggest(a,y,z):
Max=a
if y>Max:
Max=y
if z>Max:
Max=z
return Max
采纳答案by aga
It's because of indentation in your function. You've put the instruction return Max
on the most inner level of your chain of if
's, so it returns result only if the maximum is z
number. When the a
or y
is maximum it returns nothing. You can read more about python's attitude to indentation here.
这是因为您的函数中有缩进。您已将指令return Max
放在if
's链的最内层,因此仅当最大值为z
数字时才返回结果。当a
ory
为最大值时,它不返回任何内容。你可以在这里阅读更多关于 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
If you don't need to implement your own function, you can use built-in max
function, as Mingyupointed out.
如果你不需要实现你自己的函数,你可以使用内置max
函数,正如明宇所指出的。
回答by Mingyu
>>> max(2, 3, 5)
5
>>> help(max)
Help on built-in function max in module builtin:
关于模块builtin 中内置函数 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)
回答by TougherApollo1
I would suggest this...
我会建议这个...
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)
max_of_three(3,4,2)
prints 4
打印 4
回答by user6278346
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)
回答by scriptless
I'll just paste it here in case some new python student gets here.
我会把它贴在这里,以防一些新的 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))
回答by Avi Gaur
It would be better to use "max" function in python, the best thing about max is, it is not restricted to 3 or 4 arguments.
在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
回答by Yannick Assouman
def findMax(a, b,c)
return max(a, b,c)
print(findMax(6, 9, -5)
回答by Karthik Sivaraman
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)