2016-04-24 138 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