简体   繁体   English

如何使用包含 n1 到 n2 的 for 循环查找偶数

[英]How to find a even number using a for loop contains n1 to n2

Am not getting it right.我没有做对。 Why do I get an AssertionError for my function even_numbers?为什么我的 function even_numbers 会出现 AssertionError?

def even_numbers(n1, n2):
    (n1, n2) = [-2, 4]
    for num in range(n1, n2 + 0):
        
        if num % 2 == 0:
            print(num, end = " ") 

n1, n2 = [-2, 4]
assert even_numbers(-2, 4) == [-2, 0, 2]
---------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
    <ipython-input-19-6544d23ef931> in <module>
          1 # Q6 Test Cases
    ----> 2 assert even_numbers(-2, 4) == [-2, 0, 2]
    
    AssertionError

There are a few mistakes in the code.代码中有一些错误。

First, the function overrides the arguments n1, n2 by assigning [-2, 4] (perhaps a leftover of some debugging?)首先,function 通过分配[-2, 4]覆盖 arguments n1, n2 (也许是一些调试的剩余部分?)

Second, as noted in comments, the function filters adequately even numbers, but then prints them, and doesn't return anything.其次,如评论中所述,function 充分过滤偶数,然后打印它们,并且不返回任何内容。

You would like to return a list of all even numbers, so you need to collect them in a list, then return that.您想返回所有偶数的list ,因此您需要将它们收集到一个列表中,然后返回。

You can just restructure your code as follow:您可以按如下方式重组您的代码:

def even_numbers(n1, n2):
    return [num for num in range(n1, n2) if num % 2 == 0]

>>> even_numbers(-2, 4)
[-2, 0, 2]

assert even_numbers(-2, 4) == [-2, 0, 2]

Note how this is the same code as yours, and even in the same order, but turned into a list comprehension.请注意,这与您的代码如何相同,甚至顺序相同,但变成了列表理解。

You can also keep the code even closer to your original:您还可以使代码更接近原始代码:

def even_numbers(n1, n2):
    res = []
    for num in range(n1, n2):
        if num % 2 == 0:
            res.append(num)
    return res

What I first notice is that you are not returning anything from the code.我首先注意到的是您没有从代码中返回任何内容。 So, as you want to return a list, you need to have one and append the values to it.所以,当你想返回一个列表时,你需要有一个和 append 的值给它。

Also, why you added a +0 to your range limit?另外,为什么您在范围限制中添加了+0 The value will be the same.值将相同。 What you may want to do is subtract 1 because range will go from -2 to 3 if you don't do it.您可能想要做的是减去 1,因为如果您不这样做,go 的范围将从 -2 到 3。

You can do this (tried to make it simple so you understand everything in the code):您可以这样做(试图使其简单,以便您理解代码中的所有内容):

def even_numbers(n1, n2):
    even_numbers_list = []
    for number in range(n1, n2-1):
       if number % 2 == 0:
          even_numbers_list.append(number)
    return even_numbers_list

>>> even_numbers(-2, 4)
[-2, 0, 2]

Also I advice you to add some logs (or prints) so you can understand better what are the errors of your code.此外,我建议您添加一些日志(或打印),以便您更好地了解代码的错误。 Any question just ask;)任何问题都可以问;)

暂无
暂无

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

相关问题 查找数组中第n1个最小数到第n2个最小数的快速算法 - Fast algorithm to find the n1 th smallest number to n2 th smallest number in an array 通过在python中使用super()。Sub(n1,n2)出错 - Error by using super().Sub(n1,n2) in python 为什么 `n1 if eval(“n1&lt;=n2”) else n2` 总是返回 `n1`? - Why does `n1 if eval(“n1<=n2”) else n2` always return `n1`? 如何在for循环中使用多重处理? 如何使一个内核运行从索引n1到n2的循环,另一个内核从索引n2到n3的循环? - How to use multiprocessing for a for loop? How to make a core run the loop from index n1 to n2 and the other core from index n2 to n3? 为什么 n1 和 n2 之间存在差异? - Why there is a difference between n1 and n2? 如何制作一个 function,它返回列表 n 中参数 n1 或 n2 的倍数的所有整数 - How to make a function that returns all integers that are multiples of either parameters n1 or n2 within the list n csv.writer - 如何在 writerow(n1,n2,n3, …nth) 中动态写入列? - csv.writer - How to dynamically write columns in writerow(n1,n2,n3, …nth)? 如何从 python 列表中的字符串中删除 \n1、\n2、\n3 等? - How to remove \n1, \n2, \n3 etc. from a string in python list? 适用于0 ^ n1 ^ n2 ^ n的Python图灵机 - Python turing machine for 0^n1^n2^n 确定 a、b 中是否存在数字 n1、n2 和 c 中的 n3,使得 n1 + n2 = n3 [ftt,多项式乘法] - Determining if there exists numbers n1, n2 in a, b and n3 in c such that n1 + n2 = n3 [ftt, polynomial multiplication]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM