简体   繁体   English

Python:返回 None 而不是 False

[英]Python: returning None instead of False

The below code is returning None instead of False .下面的代码返回None而不是False Please help me understand the reason.请帮我理解原因。

def same_first_last(nums):
    for i in nums:
        if nums[0] == nums[-1] and len(nums) >= 1:
            return True
        else:
            return False


print(same_first_last([]))

Any help is appreaciated !!!任何帮助都会得到帮助!

There are two scenarios based on your question: -根据您的问题,有两种情况:-

  1. Size of nums > 0: nums > 0:

    If nums list is non-empty, then it will either satisfy your if condition ie nums[0] == nums[-1] and len(nums) >= 1 in which case it will return True or else it will return False如果nums列表非空,那么它将满足您的if条件,即nums[0] == nums[-1] and len(nums) >= 1在这种情况下它将返回True否则它将返回False

  2. Size of nums == 0数字大小nums 0

    If this is the case then code doesn't even go inside the for loop in which case It will not be able to go inside the if else block, that's why python returns None by default, so your code is also returning None.如果是这种情况,那么代码甚至不会在for循环中使用 go 在这种情况下,它将无法在if else块中使用 go,这就是为什么 python 默认返回None的原因。

    To avoid this you can change the code as below:- ( You don't even need to go inside the for loop as your if else conditions are static ie not changing with the iterator i from the for loop of your question.为避免这种情况,您可以按如下方式更改代码:-(您甚至不需要在 for 循环中使用 go ,因为您的if else条件是 static ,即不使用问题的for循环中的迭代器i进行更改。

def same_first_last(nums):
    return nums and nums[0] == nums[-1]

print(same_first_last([]))

here in the above return nums and nums[0] == nums[-1] , it a join of two conditions if nums and if nums[0] == nums[-1]这里在上面return nums and nums[0] == nums[-1] ,它是两个条件的连接if numsif nums[0] == nums[-1]

a) if nums is checking if the list or the string you are iterating is empty or not. a) if nums正在检查您正在迭代的列表或字符串是否为空。 If empty it return False else True如果为空,则返回False ,否则返回True

b) if nums[0] == nums[-1] : - It's checking for the condition from your question (checking if the front element and the last element are equal or not) b) if nums[0] == nums[-1] : - 它正在检查您的问题的条件(检查前面的元素和最后一个元素是否相等)

When both the above conditions met, then only it will return True or else it will return False当上述两个条件都满足时,只有它会返回 True 否则它会返回 False

Since you are calling your method with an empty list, you are never entering the for-loop in your method and therefor you can never reach your return-statement.由于您使用空列表调用您的方法,因此您永远不会在您的方法中进入 for 循环,因此您永远无法到达您的返回语句。 when your method is called with an empty List, your for-loop is skipped completely.当使用空列表调用您的方法时,您的 for 循环将完全跳过。 You can change your method like this:您可以像这样更改方法:

def same_first_last(nums): 
    for i in nums: 
        if nums[0] == nums[-1] and len(nums) >= 1: 
            return True
        else:
            return False
    return <enter default value here>

But you dont even need the loop, so you can simplify your code even a little bit more:但是您甚至不需要循环,因此您可以进一步简化代码:

def same_first_last(nums): 
    if len(nums) > 0: 
        return nums[0] == nums[-1]
    else:
        return False

You are not entering the loop, so the function effectively has no return value.您没有进入循环,因此 function 实际上没有返回值。 This can be verified by printing something inside the loop:这可以通过在循环内打印一些东西来验证:

def same_first_last(nums):
    for i in nums:
        print("in the loop")
        if nums[0] == nums[-1] and len(nums) >= 1:
            return True
        else:
            return False


print(same_first_last([]))

You can actually make this code work by swapping the conditions in the if() statement and removing the for loop like so:实际上,您可以通过交换 if() 语句中的条件并删除 for 循环来使此代码工作,如下所示:

def same_first_last(nums):
    return len(nums) >= 1 and nums[0] == nums[-1]

print(same_first_last([]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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