関数uses
は常に True を返します。正しくやれば12語しかない。
uses
Falseを返すようにするにはどうすればよいですか?
def count_vowels():
fin = open ('words.txt')
count = 0
vowels = ['aeiou']
for line in fin:
word = line.strip()
if uses(word, vowels):
count = count + 1
print "There are", count ," words with a vowel."
def uses(word, vowels):
for vowels in word:
if vowels in vowels:
return True
return False
def count_words():
fin = open ('words.txt')
vowels = ['aeiou']
for line in fin:
word = line.strip()
if uses(word, vowels):
print word
count_vowels()
count_words()
#This is what returns in the shell when I run my code:
>>>There are 1 words with a vowel.
There are 2 words with a vowel.
There are 3 words with a vowel.
There are 4 words with a vowel.
There are 5 words with a vowel.
....
#and so on...
# However, if I use the following code to replace the middle function in my original code:
def uses(word, vowels):
found = ''
for l in word:
if l not in found:
if l in vowels:
found += l
else:
return False
return found == vowels
# When I run the code the shell returns this:
>>>
>>>