我正在设计一个程序,它查看单词列表,并计算有多少单词中只有字母 p、y、t、h、o 和 n。
到目前为止,我的代码是:
def find_python(string, python):
"""searches for the letters 'python' in the word."""
for eachLetter in python:
if eachLetter not in string:
return False
return True
def main():
python = 'python'
how_many = 0
try:
fin = open('words.txt')#open the file
except:
print("No, no, file no here") #if file is not found
for eachLine in fin:
string = eachLine
find_python(string, python)
if find_python(string, python) == True:
how_many = how_many + 1#increment count if word found
print how_many#print out count
fin.close()#close the file
if __name__ == '__main__':
main()
但是,我的代码返回的单词数不正确,例如,如果我为其输入 print 语句,它将返回单词“xylophonist”,因为其中包含字母 python。我应该怎么做它会拒绝任何有禁止字母的单词?