2015-11-16 27 views
1

该代码应该要求用户选择一个词来搜索网页。我认为最简单的方法是将整个网页放在一个列表中,并找出正在搜索的单词是否在列表中。我有两个问题,第一个是:我无法将页面转换为列表。第二个问题是我无法获得正确工作的提示。对不起,我还是很新的python,任何帮助都将不胜感激。将网页转换成列表

#p6 scrabble 
#looks for a word in a giant list tells if it is present or not 
words=[] 
import urllib.request 
url='https://www.cs.uoregon.edu/Classes/15F/cis122/data/sowpods_short.txt' 

with urllib.request.urlopen(url) as webpage: #opens the webpage 
    for line in webpage: 
     line= line.strip() 
     line= line.decode('utf-8')#unicode 
     if line [0] != "#": 
      item_list =line.split(',') 
words.append(webpage) 

prompt=input("press L to search for a word or press q to quit") 
while prompt != 'q': 
    question= input("type a word to search for ")  
    if question == words: 
     print("yes, " , "was in the list") 
    elif print("not on the list") 
+0

你不能使用==来一个字比较表(字)或甚至网页再次 – furas

回答

0
import urllib.request 

words = [] 

url='https://www.cs.uoregon.edu/Classes/15F/cis122/data/sowpods_short.txt' 

with urllib.request.urlopen(url) as webpage: 
    for line in webpage: 
     line = line.strip().decode('utf-8') 
     if line[0] != "#": 
      words += line.split(',') 

print(words) 

while True: 
    question = input("type word or `q` to quit: ") 

    if question == 'q': 
     break 

    if question in words: 
     print("yes,", question, " was on the list") 
    else: 
     print("not on the list") 
+0

感谢,这就是今晚2! –