请帮助,此循环不是';我没有按预期工作,我也没有';我不知道如何在Python中使用布尔值

请帮助,此循环不是';我没有按预期工作,我也没有';我不知道如何在Python中使用布尔值,python,Python,我正在尝试编写一段python代码,计算并打印2中的1000个素数 然而,1999年是我成绩的最后一个要素。我知道以前有很多这样的问题,但我想知道为什么我的代码不起作用。 顺便问一下:如何在python中声明布尔值?连谷歌搜索都找不到线索 mylist=[2] num=1 count=0 while count<1000: if num>1: add=1 for i in mylist: if num%i==0:

我正在尝试编写一段python代码,计算并打印2中的1000个素数 然而,1999年是我成绩的最后一个要素。我知道以前有很多这样的问题,但我想知道为什么我的代码不起作用。
顺便问一下:如何在python中声明布尔值?连谷歌搜索都找不到线索

mylist=[2]
num=1
count=0
while count<1000:
    if num>1:
        add=1
        for i in mylist:
            if num%i==0:
                add=0
                break
        if add==1:
            mylist=mylist+[num]
    num=num+2
    count=count+1
print mylist
mylist=[2]
num=1
计数=0
而第1项:
相加=1
对于mylist中的i:
如果num%i==0:
相加=0
打破
如果add==1:
mylist=mylist+[num]
num=num+2
计数=计数+1
打印mylist

您的循环应在len(mylist)<1000:时读取
,并删除对
计数的所有引用。或者,每次向列表中添加素数时,只应递增
count

此外,在Python中不“声明”值。您只需将所需类型的值分配给变量,然后poof,您就有了该类型的变量。在这种情况下,
True
False
是布尔值,您可以将它们分配给变量

以下是我的惯用Python版本(即,以有经验的Python程序员可能编写的方式编写):

primes=[2]
候选人=3
而len(素数)<1000:
isprime=True
对于素数中的testprime:
如果候选%testprime==0:
isprime=False
打破
如果是iPrime:
素数。追加(候选)
候选人=候选人+2
打印素数
如果您想用更简洁、更快的Python编写,请执行以下操作:

import itertools
primes = [2]
candidate_iter = itertools.count(3, 2)
while len(primes) < 1000:
    candidate = candidate_iter.next()
    if all(candidate % testprime != 0 for testprime in primes):
        primes.append(candidate)
print primes
导入itertools
素数=[2]
候选项=itertools.count(3,2)
而len(素数)<1000:
candidate=candidate\u iter.next()
如果所有(候选%testprime!=0表示素数中的testprime):
素数。追加(候选)
打印素数

问题在于,您正在为集合1,3,5,…,2001中的每个数字增加变量
count
。相反,只有当
add==1

以下代码与后代代码相同:

primes = [2]                              # initial list of primes 
candidate = 1                             # let's start at 1, could start at 3 as well
primesFound = 1                           # We've actually found one already
while primesFound <= 1000:                # continue till 1000 primes have been found
                                          # using a counter instead of len(primes) 
                                          # maybe faster
    if candidate > 1:                     # 1 is not a prime
        isPrime = True                    
        for i in primes:                  # iterate over all primes already found 
            if candidate % i == 0:        # primes should not be divisable
                isPrime = False           # not a prime
                break                     # no use going through the rest of the primes
        if isPrime:                       # candidate is a prime so
            primes = primes + [candidate] # add it to the primes list
            primesFound += 1              # increment the prime counter
    candidate += 2                        # we can skip all even numbers 
print primes                              # done.
primes=[2]#primes的初始列表
候选人=1#让我们从1开始,也可以从3开始
primesFound=1#实际上我们已经找到了一个
而素数查找1:#1不是素数
isPrime=True
对于素数中的i:#迭代所有已找到的素数
如果候选%i==0:#素数不应可除
isPrime=False#不是质数
打断#通过其余的素数没有用
如果isPrime:#候选人是素数,那么
素数=素数+[候选]#将其添加到素数列表中
primesFound+=1#增加prime计数器
候选人+=2#我们可以跳过所有偶数
打印素数#完成。

如果您打算使用Python,学习Python习惯用法是一件好事。我在下面使用了一些,包括
itertools.count
,使用
not
测试某个值是否等于零,
else
循环子句,以及负索引来获取列表的最后一项

另外,Python标准是使用四个空格缩进,如下所示;运算符周围的空格,如
=
==
%
;和逗号后的空格

from itertools import count
mylist = [2]
# This will go up by 2s starting from 3: 3, 5, 7, 9...
for num in count(3, 2):
    # For each prime already found
    for i in mylist:
        # if the prime is a factor of the number
        if not num % i:
            # don't add it
            break
    else:
        # if none of the primes were factors
        # add this number to the list of primes
        mylist.append(num)
        # if there are 1000 numbers in the list, we're done
        if len(mylist) == 1000:
            break
# print the 1000th prime
print mylist[-1]

这些是你的提示。这些是关于Python的提示。有问题吗?“我如何在python中声明布尔值?”——通过在google中键入这句话:我用两个惯用python示例更新了我的答案。一个简单地使用bool,只需要知道您已经知道(或者应该知道,因为您使用的是列表)的结构。另一个需要一些迭代器工作原理的知识和迭代器的理解才能理解。@beautCode:噢,糟糕。害羞的露齿一笑修正了这一点。:-)@Omnifarious:这不是完全惯用的Python。惯用Python会扔掉
isprime=True
isprime=False
行,并将
如果isprime:
更改为
else:
(可能是
else:#prime
)。它会将
candidate=candidate+2
更改为
candidate+=2
@Chris Morgan:也许有些人会。我以前从未见过这种构造,现在我不喜欢它了。我确实同意
+=
,但我不想在OP中引入很多新的东西,因为OP正在学习。
for..否则
块可以大大改进某些类型的代码。当你第一次看到它时,你会很好奇到底发生了什么,但这是一个很好的结构,真的。这是一个案例的例子,它通过使用它得到了改进(正如@agf在他的回答中所说的)。@Chris Morgan:事实上,在这里使用的构造是错误的。正确的结构是使用
all
,它更快、更清晰、更短。
from itertools import count
mylist = [2]
# This will go up by 2s starting from 3: 3, 5, 7, 9...
for num in count(3, 2):
    # For each prime already found
    for i in mylist:
        # if the prime is a factor of the number
        if not num % i:
            # don't add it
            break
    else:
        # if none of the primes were factors
        # add this number to the list of primes
        mylist.append(num)
        # if there are 1000 numbers in the list, we're done
        if len(mylist) == 1000:
            break
# print the 1000th prime
print mylist[-1]