以下python代码的问题 [英] Problem with following python code

查看:43
本文介绍了以下python代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下代码问题。它应该是打印10到100之间的素数。但是我没有得到任何

输出,即我想外面的''为' '循环仅被遍历

一次。如果你能帮助我,我会很高兴。感谢名单!

I''ve been having problem with the following code. It''s supposed to
print the prime numbers between 10 and 100. But i''m not getting any
output, i.e. i guess the outer ''for'' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!


>> f = 1
for i in range(10,100) :
>>f=1
for i in range(10,100):



.... for j in range(2,i):

...如果我%j == 0:

.... f = 0

....破解

....其他:继续

....如果f == 1:

....打印我,

....

推荐答案

2007年6月12日,为什么? < ji ******* @ yahoo.comwrote:
On 6/12/07, why? <ji*******@yahoo.comwrote:

我遇到以下代码问题。它应该是打印10到100之间的素数。但是我没有得到任何

输出,即我想外面的''为' '循环仅被遍历

一次。如果你能帮助我,我会很高兴。感谢名单!
I''ve been having problem with the following code. It''s supposed to
print the prime numbers between 10 and 100. But i''m not getting any
output, i.e. i guess the outer ''for'' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!

> f = 1
我在范围内(10,100):
>f=1
for i in range(10,100):



你需要切换这两行来重置每一次的标志

外循环。


干杯,


Tim

You need to switch these two lines to reset the flag each time around
the outer loop.

Cheers,

Tim


... for j in range(2,i):

...如果我%j == 0:

... f = 0

...休息

......否则:继续

...如果f == 1:

...打印我,

...


-
http://mail.python.org/mailman/listinfo/python-list


En Tue,2007年6月12日01:25: 31-0300,为什么? < ji ******* @yahoo.comescribió:
En Tue, 12 Jun 2007 01:25:31 -0300, why? <ji*******@yahoo.comescribió:

我遇到以下代码问题。它应该是打印10到100之间的素数。但是我没有得到任何

输出,即我想外面的''为' '循环仅被遍历

一次。如果你能帮助我,我会很高兴。感谢名单!
I''ve been having problem with the following code. It''s supposed to
print the prime numbers between 10 and 100. But i''m not getting any
output, i.e. i guess the outer ''for'' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!

>>> f = 1
我在范围内(10,100):
>>>f=1
for i in range(10,100):



...对于范围内的j(2,i):

...如果i%j == 0:

... f = 0

...休息

...其他:继续

...如果f == 1:

...打印我,

...

... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...



请注意,一旦设置f = 0,它永远不会改变。

在外循环内移动f = 1。另外,else子句在这里没用;

最好使用bool作为条件;并且它将受益于更好的变量名称
。保持相同的结构:


范围内的数字(10,100):

is_prime = True

为范围内的除数(2) ,数字):

如果数字%divisor == 0:

is_prime = False

break

if is_prime :

打印号码,


下一步:for循环有一个可选的else当循环正常退出时(在这种情况下,当除数达到
数时,并且从不执​​行break语句),子句执行

。所以你不需要

is_prime:


范围内的数字(10,100):

范围内的除数( 2,号码):

如果号码%divisor == 0:

休息

否则:

打印号码,


-

Gabriel Genellina

Note that once you set f=0, it will never change.
Move the f=1 inside the outer loop. Also, the else clause is useless here;
best to use a bool for conditions; and it would benefit from better
variable names. Keeping the same structure:

for number in range(10,100):
is_prime = True
for divisor in range(2,number):
if number % divisor == 0:
is_prime = False
break
if is_prime:
print number,

Next step: for loops have an optional "else" clause, that gets executed
whenever the loop exits normally (in this case, when divisor goes up to
number, and the break statement is never executed). So you don''t need
is_prime:

for number in range(10,100):
for divisor in range(2,number):
if number % divisor == 0:
break
else:
print number,

--
Gabriel Genellina


6月12日星期二, 2007年上午04:25:31 -0000,为什么?写道:
On Tue, Jun 12, 2007 at 04:25:31AM -0000, why? wrote:

我遇到以下代码问题。它应该是打印10到100之间的素数。但是我没有得到任何

输出,即我想外面的''为' '循环仅被遍历

一次。如果你能帮助我,我会很高兴。感谢名单!
I''ve been having problem with the following code. It''s supposed to
print the prime numbers between 10 and 100. But i''m not getting any
output, i.e. i guess the outer ''for'' loop is being traversed only
once. I would be greatful if you could help me out. Thanx!

> f = 1
我在范围内(10,100):
>f=1
for i in range(10,100):



...对于范围内的j(2,i):

...如果i%j == 0:

... f = 0

...休息

...其他:继续

...如果f == 1:

...打印i,

...

... for j in range(2,i):
... if i%j==0:
... f=0
... break
... else: continue
... if f==1:
... print i,
...



移动f = 1在外循环内:


for i in range(10,100):

f = 1

for j in range(2, i):

if i%j == 0:

f = 0

break

else:continue

如果f == 1:

打印i,


在第一次迭代中它被设置为0并且再也没有机会了

之后设为1.

Move "f=1" inside the outer loop:

for i in range(10,100):
f=1
for j in range(2,i):
if i%j==0:
f=0
break
else: continue
if f==1:
print i,

It gets set to 0 in the first iteration and never has another chance to
be set to 1 after that.


这篇关于以下python代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆