我被要求编写一个代码,该代码将打印“dictionary.txt”(一个 250,000 个单词的文件)中的单词,该单词仅包含一个元音,没有字母“s”,长度为 7 个字母。我知道我必须定义一个打开文件并搜索这些要求的函数。
我不允许使用正则表达式,并且文件是每行一个单词。
这是我当前的python脚本:
a="a"
e="e"
i="i"
o="o"
u="u"
y="y"
def search():
Input=open("dictionary.txt","r")
for word in Input:
word=Input.lower()
vowel=len(word-a)==6 or len(word-e)==6 or len(word-i)==6 or len(word-o)==6 or len(word-u)==6 or len(word-y)==6
if len(word)==7 and "s" not in word and vowel==True:
return word
print(search())