4 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
你用all()錯(cuò)了。 all(letters)始終是一個(gè)Truefor string letters,并True in <string>返回一個(gè)TypeError.
你應(yīng)該做的是:
all(x in word for x in letters)
于是,就變成了:
for word in words:
if all(x in word for x in letters):
print(word)

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
由于代碼中有很多語(yǔ)法錯(cuò)誤,我正在嘗試重寫您提供的代碼,以粗略地描繪出您的目標(biāo)。我希望下面的代碼能夠滿足您的需求。
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貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
如果您省略,則更簡(jiǎn)單的解決方案all是:
letters = input('letters: ')
words_in_file = open('thesewords').read().splitlines()
for word in words_in_file:
if letters in words:
print(word)

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
嘗試這個(gè):
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)
添加回答
舉報(bào)