newbie question: for loop within for loop confusion

takayuki lawtonpaul at gmail.com
Mon Jun 16 03:23:44 CEST 2008


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



More information about the Python-list mailing list