【问题标题】:Finding prime numbers, I wrote an incorrect function and a correct one. But I am not sure why one is correct and one isn't找到素数,我写了一个不正确的函数和一个正确的函数。但我不确定为什么一个是正确的,一个是不正确的
【发布时间】:2020-11-30 15:49:30
【问题描述】:

所以我们的想法是创建一个函数来返回任意两个数字(包括)之间的所有素数。 下面,我把我写对的函数和我写错的函数放在一起。

我犯的错误是“else”语句的缩进。但我不知道为什么一个是正确的,另一个是不正确的!

我也很好奇我从不正确的函数得到的输出,它主要显示素数的重复,但偶尔会抛出一个不是素数的数字,而是一个可被 3 整除的数字和另一个素数数字。我一直在试图弄清楚这个函数是如何产生这个结果的(所以我可以理解这个错误),但我很难过!

任何帮助将不胜感激

##This is the correct version: ##########################

def primefinder(start, end):                   
    primes = []
    for number in range(start, end +1):
        if number > 1:
            for i in range(2, number):
                if number % i == 0:
                    break
            else:                            
                primes.append(number)
    return primes


## This is the incorrect version:##############

def primefinder(start, end):
    primes = []
    for number in range(start, end +1):
        if number > 1:
            for i in range(2, number):
                if number % i == 0:
                    break
                else:                     ##<--This is my mistake##
                    primes.append(number)
    return primes

【问题讨论】:

  • 我强烈建议在图形调试器中打开您的程序,并在查看局部变量值列表(inumber 等)的同时单步执行它。一个常见的免费选择是使用 PyCharm,但它绝不是唯一的。学习使用调试器是一项重要的技能,当您开始处理太长而无法适应 StackOverflow 问题的程序时,它将为您提供很好的帮助。

标签: python function loops primes


【解决方案1】:

Python 对缩进敏感。如果更改缩进,则代码本身会更改。您的第一个代码具有对应于forelse,创建一个for...else 循环,如果循环没有中断(如果循环遇到break 这意味着一个因素被发现所以它不是素数)。

在您的错误版本中,else 额外缩进了一个,因此它对应于if,这意味着只要该数字具有不可被整除的因素(例如 8 不能被可被 3 整除),然后它将附加到列表中,对于每个不能被整除的因素一次。

【讨论】:

    【解决方案2】:

    任何帮助将不胜感激

    如有疑问,您可以在交互模式下使用help。在这种情况下,启动 python 并执行help("for") 以获取以下信息:

    The "for" statement
    *******************
    
    The "for" statement is used to iterate over the elements of a sequence
    (such as a string, tuple or list) or other iterable object:
    
       for_stmt ::= "for" target_list "in" expression_list ":" suite
                    ["else" ":" suite]
    
    The expression list is evaluated once; it should yield an iterable
    object.  An iterator is created for the result of the
    "expression_list".  The suite is then executed once for each item
    provided by the iterator, in the order returned by the iterator.  Each
    item in turn is assigned to the target list using the standard rules
    for assignments (see Assignment statements), and then the suite is
    executed.  When the items are exhausted (which is immediately when the
    sequence is empty or an iterator raises a "StopIteration" exception),
    the suite in the "else" clause, if present, is executed, and the loop
    terminates.
    ...
    

    等等,请注意,这仅在迭代因耗尽而结束时才有效,而不是当您从循环中break-ed 时。考虑一下

    for x in [1,2,3]:
        print(x)
    else:
        print("else")
    

    最后会打印“else”,而

    for x in [1,2,3]:
        print(x)
        break
    else:
        print("else")
    

    不会。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2013-11-29
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多