[英]Python: don't understand difference
I am really sorry about my python understanding and about my English.对于我对 python 的理解和我的英语,我真的很抱歉。 I just started to learn Python and really dont understand difference between two next code:
我刚开始学习 Python 真的不明白下两个代码之间的区别:
def arrayCheck(nums):
"""
find nums
"""
for i in range(len(nums)-2):
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
return True
return False
result = arrayCheck([1, 1, 2, 3, 1])
print(result)
When run this code the result is True运行此代码时,结果为 True
And the next one:下一个:
def arrayCheck(nums):
"""
find nums
"""
for i in range(len(nums)-2):
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
return True
else:
return False
result = arrayCheck([1, 1, 2, 3, 1])
print(result)
The second code return False.第二个代码返回 False。
Why?为什么? Thanks in advance.
提前致谢。
The first code can return only True
or None
.第一个代码只能返回
True
或None
。 After the return statement the function ends so the first code never reach the return False
statement.在 return 语句之后 function 结束,因此第一个代码永远不会到达
return False
语句。 The second code will return False
if the first 3 items are not 1, 2 and 3 since if the condition does not hold it returns False
.如果前 3 个项目不是 1、2 和 3,则第二个代码将返回
False
,因为如果条件不成立,则返回False
。
I would actually assume this is the code you are interested in -我实际上会假设这是您感兴趣的代码 -
def arrayCheck(nums):
"""
find nums
"""
for i in range(len(nums)-2):
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
return True
return False
In python the indentation matters, and return will break out the function so, in the first codeblock you have在 python 中,缩进很重要,返回将打破 function 所以,在第一个代码块中你有
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
return True
return False
The two return
are on the same indent level so if the condition is met It will go to first line, return True
, see return and break out of the function ignoring everything afterwards but in the second codeblock you have两个
return
处于相同的缩进级别,因此如果满足条件,它将 go 到第一行, return True
,查看返回并突破 function 之后忽略所有内容,但在第二个代码块中你有
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
return True
else:
return False
so if the condition is true it will return true and break out of the function but if the condition is not true it will return false and break out of the function so it only does one of the iterations you are trying to do.因此,如果条件为真,它将返回真并突破 function 但如果条件不真,它将返回假并突破 function,因此它只执行您尝试执行的迭代之一。
If I understand what you are trying to do correctly, this would be the solution:如果我理解您要正确执行的操作,这将是解决方案:
def arrayCheck(nums):
"""
find nums
"""
found = False
for i in range(len(nums)-2):
if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
found = True
return found
result = arrayCheck([1, 1, 2, 3, 1])
print(result)
This works because it allows the function to check over every iteration in the for loop and it will return true if the numbers were found这是有效的,因为它允许 function 检查 for 循环中的每次迭代,如果找到数字,它将返回 true
In the first code, if condition has no else part.在第一个代码中, if条件没有 else 部分。 While in the 2nd code, if condition has a else part.
在第二个代码中, if条件有一个 else 部分。 So when if condition is false in the first code, it is going for second iteration and if condition getting True as per the input and it is returning True.
因此,当第一个代码中的条件为假时,它将进行第二次迭代,并且如果条件根据输入为真,则返回真。 But in the second code, if condition is false and it is going to else part and returning False.
但是在第二个代码中,如果条件为假,它将进入 else 部分并返回 False。
In first code return False
will never execute because it is inside the if condition and before it another return
statement return True
.在第一个代码中
return False
永远不会执行,因为它在 if 条件内,并且在它之前另一个return
语句return True
。 After returning the function execution will stop.返回 function 后,执行将停止。
In the second code if condition fails for the first time the function execution will stop because of return False
in else condition.在第二个代码中,如果条件第一次失败,function 执行将停止,因为在 else 条件下
return False
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.