新手问题:for循环混乱中的循环 [英] newbie question: for loop within for loop confusion

查看:88
本文介绍了新手问题:for循环混乱中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在通过优秀的书如何像蟒蛇一样思考

程序员来学习python。作者:Allen Downey。


Noob问题如下......


animals.txt是一个动物列表,每个都在一个单独的行上: ; aardvard,

bat,cat,dog,elephant,fish,giraffe,horse,insect,jackelope"


我想循环遍历单词列表并且打印不要b $ b的单词有任何避免的单词。在他们的信中。


def hasnolet(避免):

fin = open(''animals.txt'')

for line in fin:

word = line.strip()

表示避免:

如果是字母:

休息

其他:

打印字

hasnolet(''abcd'')


为什么上面的功能不起作用?它返回:























inchworm

inchworm

感谢您的帮助。


takayuki

解决方案

文章

< a0 **************** ****************** @ g16g2000pri。 googlegroups.com>,

takayuki< la ******** @ gmail.comwrote:





我正在通过优秀的书如何像蟒蛇一样思考

程序员来学习python。作者:Allen Downey。


Noob问题如下......


animals.txt是一个动物列表,每个都在一个单独的行上: ; aardvard,

bat,cat,dog,elephant,fish,giraffe,horse,insect,jackelope"


我想循环遍历单词列表并且打印不要b $ b的单词有任何避免的单词。在他们的信中。


def hasnolet(避免):

fin = open(''animals.txt'')

for line in fin:

word = line.strip()

表示避免:

如果是字母:

休息

其他:

打印字

hasnolet(''abcd'')



我可以给你一条鱼,或者我可以教你钓鱼。我宁愿做

后者。所以...


带上你的内循环:


为避免的字母:

如果是字母:

休息

否则:

打印字



并检测它,以便您可以准确地看到发生了什么。尝试一下

喜欢:


表示避免:

print" letter =%s" %letter

如果是字母:

print" it'in the word"

break

否则:

打印不,它不是

打印字



看看是否帮助。


09:23,lu ?? 16 giugno 2008 takayuki写道:


word = line.strip()



尝试

word = line.split()


并在循环结束时再添加一个打印到新行。

-

Mailsweeper主页: http://it.geocities .com / call_me_not_now / index.html


takayuki写道:


对于避免的信件:

如果信中的字母:

break

else:

打印字



比如说狗这个词。上面的循环是做什么的

基本上这样:


1.信中避免使用''a''首先

2.''狗'中是''a''?

3.不,所以打印''狗''

4.回到循环,使用''b''

5.''b''在''狗'中?

6.不,所以打印''狗' '再次

7.回到for循环.....


因为它顺序通过''abcd'',它会说是第一个

三个字母不在''狗'中,因此打印三次。然后

它终于看到'd''*是*在狗中,所以它第四次跳过它通过循环获得


Hi,

I''m studying python via the exellent book "How to think like a python
programmer" by Allen Downey.

Noob question follows...

animals.txt is a list of animals, each on a separate line: "aardvard,
bat, cat, dog, elephant, fish, giraffe, horse, insect, jackelope"

I want to loop through the list of words and print words that don''t
have any "avoid" letter in them.

def hasnolet(avoid):
fin = open(''animals.txt'')
for line in fin:
word = line.strip()
for letter in avoid:
if letter in word:
break
else:
print word

hasnolet(''abcd'')

Why doesn''t the function above work? It returns:

dog
dog
dog
fish
fish
fish
fish
horse
horse
horse
horse
inchworm
inchworm
thanks for any help.

takayuki

解决方案

In article
<a0**********************************@g16g2000pri. googlegroups.com>,
takayuki <la********@gmail.comwrote:

Hi,

I''m studying python via the exellent book "How to think like a python
programmer" by Allen Downey.

Noob question follows...

animals.txt is a list of animals, each on a separate line: "aardvard,
bat, cat, dog, elephant, fish, giraffe, horse, insect, jackelope"

I want to loop through the list of words and print words that don''t
have any "avoid" letter in them.

def hasnolet(avoid):
fin = open(''animals.txt'')
for line in fin:
word = line.strip()
for letter in avoid:
if letter in word:
break
else:
print word

hasnolet(''abcd'')

I could give you a fish, or I could teach you to fish. I''d rather do the
latter. So...

Take your inner loop:

for letter in avoid:
if letter in word:
break
else:
print word

and instrument it so you can see exactly what''s happening. Try something
like:

for letter in avoid:
print "letter = %s" % letter
if letter in word:
print "it''s in word"
break
else:
print "no it''s not"
print word

and see if that helps.


On 09:23, luned?? 16 giugno 2008 takayuki wrote:

word = line.strip()

Try
word= line.split()

and at the end of the loop add one more print to go to new line.
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html


takayuki wrote:

for letter in avoid:
if letter in word:
break
else:
print word

Take the word ''dog'', for example. What the above loop is doing is
basically this:

1. for letter in avoid uses ''a'' first
2. is ''a'' in ''dog''?
3. no, so it prints ''dog''
4. go back to for loop, use ''b''
5. is ''b'' in ''dog''?
6. no, so it prints ''dog'' again
7. go back to for loop.....

Since it goes sequentially through ''abcd'', it will say that the first
three letters are not in ''dog'', and therefore print it three times. Then
it finally sees that ''d'' *is* in dog, so it skips it the fourth time
through the loop.


这篇关于新手问题:for循环混乱中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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