简体   繁体   中英

Python: don't understand difference

I am really sorry about my python understanding and about my English. I just started to learn Python and really dont understand difference between two next code:

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

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.

Why? Thanks in advance.

The first code can return only True or None . After the return statement the function ends so the first code never reach the return False statement. 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 .

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

See Python control flow here .

In python the indentation matters, and return will break out the function so, in the first codeblock you have

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

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.

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

In the first code, if condition has no else part. While in the 2nd code, if condition has a else part. 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.

In first code return False will never execute because it is inside the if condition and before it another return statement return True . After returning the function execution will stop.

In the second code if condition fails for the first time the function execution will stop because of return False in else condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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