简体   繁体   中英

I am not able to fix a code issue in python

I am trying to define 2 functions, but only has_33 is working and myfunction is not working.

I am trying this code in jupyter notebook:

def myfunction(num):
    for i in range(0, len(num)-1):
        if num[i:i+2] == [3,3]:
            return True
        return False

and this code:

def has_33(nums):
    for i in range(0, len(nums)-1):
        if nums[i:i+2] == [3,3]:
            return True  
    return False

myfunction([1,2,4,3,3]) should give true but it is giving false result but has_33([1,2,4,3,3]) is giving the true result. Why is this happening?

Hi there is indent difference in both code: in first function second return is inside of for loop where as in second function it is out of for loop:

So in first function when if condition is false and it is going on second return and returning false for first value 0

In second function if evaluation keep false till i is 3 and for loop is not executing return. Once if evaluation become true on i=0 it is executing return of if and returning true so control goes out of function and second return out of for is not getting executed:

corrected first function:

def myfunction(num):
    for i in range(0,len(num)-1):
        if num[i:i+2] == [3,3]:
            return True
    return False

Indentation error! Just needed to erase a whitespace from the last line of your first code. The for loop will return False now. Try like this:

def myfunction(num):  
    for i in range(0,len(num)-1):  
        if num[i:i+2] == [3,3]:  
            return True  
    return False  

Posting my comment as an answer as suggested by @quamrana.

This behavior is because of indentation. In myfunction , if the condition nums[0:2] == [3,3] is not satisfied, then the function immediately returns False . Whereas in has_33 , it iterates through entire list and then only will return False if there are no consecutive [3,3] .

eg

nums = [1,2,3,3,5]

myfunction(nums)
False

Let's go step by step in the function

  1. for i in range(0,len(num)-1)

    i is initialized to 0 , ie i = 0 .

  2. nums[i:i+2]

    Since i is 0 , becomes nums[0:2] ie [nums[0], nums[1]] .

  3. if num[i:i+2] == [3,3]

    Becomes if num[0:2] == [3,3] . Since nums[0] = 1 and nums[1] = 2 , [nums[0], nums[1]] != [3,3] . Thus, if block will not be executed.

  4. Since return False is in the for loop and if condition was not satisfied, the next line is executed, which is return False . The function execution stops here.

Now, second function:

nums = [1,2,3,3,5]

has_33(nums)
True

Step by step

  1. Same as myfunction.
  2. Same as myfunction.
  3. Same as myfunction.
  4. Now here is the catch. Since return False is written outside for loop, i increases by 1 . i = 1
  5. nums[i:i+2] is nums[1:3] which is [nums[1], nums[2]] .
  6. The loop continues until you get a [3,3] OR i = len(nums) - 1 .

Hope this helps you understand what went wrong.

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