[英]Return None instead of False
I have this code that should return false at the end of the function if one of the condition isn't met, but the output keeps saying None, why so?如果不满足其中一个条件,我有这段代码应该在 function 的末尾返回 false,但是 output 一直说无,为什么会这样?
def check_largest_and_smallest():
case1 = largest_and_smallest(17, 1, 6)
case2 = largest_and_smallest(1, 16, 6)
case3 = largest_and_smallest(1, 1, 2)
case4 = largest_and_smallest(1, 1, 1)
case5 = largest_and_smallest(-3, -4, 0)
if case1 == (17, 1):
if case2 == (17, 1):
if case3 == (2, 1):
if case4 == (1, 1):
if case5 == (0, -4):
return True
else:
return False
The largest_and_smallest
function is:最大和最小的
largest_and_smallest
是:
def largest_and_smallest(num1, num2, num3):
largest = None
smallest = None
if (num1 >= num2) and (num1 >= num3):
if num2 <= num3:
largest = num1
smallest = num2
else:
largest = num1
smallest = num3
elif (num2 >= num1) and (num2 >= num3):
if num1 <= num3:
largest = num2
smallest = num1
else:
largest = num2
smallest = num3
elif (num3 >= num1) and (num3 >= num2):
if num1 <= num2:
largest = num3
smallest = num1
else:
largest = num3
smallest = num2
return largest, smallest
The reason is due to the use of else
: as soon as the first if
statement is checked, there's no else
for the other situations, so the function simply returns None
.原因是由于使用了
else
:一旦检查了第一个if
语句,其他情况就没有else
,所以 function 只返回None
。
case1 == (17, 1)
case2 == (16, 1)
if case1 == (17, 1):
if case2 == (17, 1):
# here case2 doesn't match, but there's no corresponding else!
else:
return False
A simple fix is to just return False without any else
:一个简单的解决方法是只返回 False 而没有其他任何
else
:
if case1 == (17, 1):
if case2 == (17, 1):
if case3 == (2, 1):
if case4 == (1, 1):
if case5 == (0, -4):
return True
return False
Or, even better, use all(
which checks all iterable items and returns iterable )
False
as soon as one of them is not True
, meaning that the function will stop evaluating the following items whenever that happens:或者,更好的是,使用
all( iterable )
检查所有可迭代项并在其中一项不为True
时立即返回False
,这意味着 function 将在发生这种情况时停止评估以下项:
return all((case1 == (17, 1), case2 == (17, 1), case3 == (2, 1),
case4 == (1, 1), case5 == (0, -4)))
At a glance, for me, the indentation for check_largest_and_smallest() is not correct.乍一看,对我来说, check_largest_and_smallest() 的缩进是不正确的。 Then, you should not use else, just return in the end will satisfy your intended logic.
然后,你不应该使用 else,只要 return 最终会满足你的预期逻辑。
def check_largest_and_smallest():
case1 = largest_and_smallest(17, 1, 6)
case2 = largest_and_smallest(1, 17, 6)
case3 = largest_and_smallest(1, 1, 2)
case4 = largest_and_smallest(1, 1, 1)
case5 = largest_and_smallest(-3, -4, 0)
if case1 == (17, 1):
if case2 == (17, 1):
if case3 == (2, 1):
if case4 == (1, 1):
if case5 == (0, -4):
return True
return False
def largest_and_smallest(num1, num2, num3):
"""
Takes 3 numbers as arguments and returns
the largest number and smallest number among them.
"""
largest = None
smallest = None
if (num1 >= num2) and (num1 >= num3):
if num2 <= num3:
largest = num1
smallest = num2
else:
largest = num1
smallest = num3
elif (num2 >= num1) and (num2 >= num3):
if num1 <= num3:
largest = num2
smallest = num1
else:
largest = num2
smallest = num3
elif (num3 >= num1) and (num3 >= num2):
if num1 <= num2:
largest = num3
smallest = num1
else:
largest = num3
smallest = num2
return largest, smallest
check_largest_and_smallest()
Consider what happens if case1
is true but case2
isn't.考虑一下如果
case1
为真但case2
不是会发生什么。
Assuming that if
ladder indentation is what you intend, you're only returning false if case1
isn't true, but if case1
is true and any of the other cases are false, your code returns nothing.假设
if
梯形缩进是您想要的,那么您只会在case1
不为真时返回 false,但如果case1
为真且任何其他案例为假,则您的代码不会返回任何内容。
Try this:尝试这个:
if case1 == (17, 1) and case2 == (17, 1) \
and case3 == (2, 1) \
and case4 == (1, 1) \
and case5 == (0, -4):
return True
else:
return False
For the minimum/maximum function, try something along the lines of:对于最小/最大 function,请尝试以下方式:
def minmax(*x):
return min(x),max(x)
In any case, you can always write无论如何,你总是可以写
a < b < c
instead of代替
a<b and b<c
if check_largest_and_smallest == False:
print ("false")
else:
print ("true")
with that you can also check the value of the Boolean:) I liked that I could learn also how Python works with interrogates like that.有了它,您还可以检查 Boolean 的值:) 我喜欢我还可以了解 Python 如何与这样的询问一起工作。 So thanks to the ppl.
所以感谢ppl。 that solved it:)
解决了它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.