4 回答
TA貢獻1821條經驗 獲得超5個贊
你用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)
添加回答
舉報
