该函数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:
>>>
>>>