4 回答

TA贡献1821条经验 获得超4个赞
你用all()错了。 all(letters)始终是一个Truefor string letters,并True in <string>返回一个TypeError.
你应该做的是:
all(x in word for x in letters)
于是,就变成了:
for word in words:
if all(x in word for x in letters):
print(word)

TA贡献1900条经验 获得超5个赞
由于代码中有很多语法错误,我正在尝试重写您提供的代码,以粗略地描绘出您的目标。我希望下面的代码能够满足您的需求。
letters = input("letters:" )
words = open("thesewords.txt","r")
for word in line.split():
print (word)
print(".......................")
for wrd in words:
if letters in wrd:
print(wrd)
else:
continue

TA贡献1775条经验 获得超11个赞
如果您省略,则更简单的解决方案all是:
letters = input('letters: ')
words_in_file = open('thesewords').read().splitlines()
for word in words_in_file:
if letters in words:
print(word)

TA贡献1784条经验 获得超2个赞
尝试这个:
letters = input('letters: ')
# Make sure you include the full file name and close the string
# Also, .readlines() is simpler than .read().splitlines()
words = open('thesewords.txt').readlines()
# I'll assume these are the words:
words = ['spam', 'eggs', 'cheese', 'foo', 'bar']
print(words)
print(".......................")
for word in words:
if all(x in word for x in letters):
print(word)
添加回答
举报