2016-04-24 141 views
0

我已經創建了該程序,但面臨一些錯誤。其顯示包含從用戶輸入任何字母,但我想它應該在搜索詞的所有字母的那些話: 我的代碼是從單詞表和打印單詞中搜索字母

life=open('/Users/tim/Desktop/words.txt') 
dds=str(input()) 
for list in life: 
    index=0 


    if dds[index] in list: 
     print(list) 
    index=index+1 

我不得不這樣做,因爲我想它可以搜索哪些修改?

而且我想這:

life=open('/Users/tim/Desktop/words.txt') 
    dds="er" 
    for list in life: 
     index=0 


     if dds[index] in list: 
      print(list) 
     index=index+1 

現在,我想它應該打印包含字的 「e」

回答

0

嘗試所有這一切的話:

if dds[index:] in list: 

你在做:if dds[index] in list:它意味着它將打印wordlist中包含任何字詞「dds」的所有單詞。如果你試試這個:if dds[index:] in list:它會搜索wordlist中的所有單詞。 [:]將選擇所有字母

a[start:end] # items start through end-1 
a[start:] # items start through the rest of the array 
a[:end]  # items from the beginning through end-1 
a[:]   # a copy of the whole array 
+2

你能解釋一下你的答案嗎? – user325923

+0

你在做: '如果列表中的dds [index]:'這意味着它將打印所有在wordlist中的單詞中包含任何字母「dds」的單詞。 如果你試試這個: 'if dds [index:] in list:' 它會搜索wordlist中的所有字母。 [:]將選擇所有的字母 –

+0

感謝它的完美工作:) – user325923