2017-02-12 19 views
0

私は最近このコードを見つけましたが、それがどのように機能するかを考えるのに苦労しています。 "111.txt"は、各行のリストの最初の部分が解であり、そのリスト内の対応する単語がその解で行うキーワードのリストを含むテキストファイルです。私は8行目以外のほとんどを理解しています(solutions[i[-1]] ...)。私は使用されているdifferntモジュールを探していましたが、私はまだその行が何をし、どのように動作するのか理解していません。可能であれば、この行について簡単に説明していただきたいと思います。テキストファイルの最初の単語を抽出し、それに対応する単語を抽出しますか?

ありがとうございます! "112.txt" の

questionbank = [] 
with open ('111.txt') as questions: 
    for line in questions: 
     questionbank.append(line.strip().split(',')) 

solutions = {} 
for i in questionbank: 
    solutions[i[-1]] = i[0:len(i)-1] 

def phone_problem(): 
    n = 2 
    while n <3: 
     problem = input("Phone problem?") 
     for d,v in solutions.items(): 
      if any(word in problem for word in v): 
       print(d) 
       n = 4 
      else: 
       continue 

phone_problem() 

例:

put your phone is rice, wet, damp, water, puddle 
replace you screen, screen, crack, smash, shatter... 

UPDATE: 私はあなたのコードに追加したが、それはまだ解決策出力をdoes't。問題として私が何を入力しても、whileループを実行し続けるだけです。私は本当に理由は分かりませんが、あなたが解決策を定義したときにそれが関係するかもしれません。

import webbrowser,time 
url = "https://www.google.co.uk/webhp?hl=en&sa=X&ved=0ahUKEwjNuLiL1vHRAhVjD8AKHdFEAiEQPAgD&gws_rd=cr&ei=zUiTWKKpF8P_UoSambgO#hl=en&q=" 
problem = input("What is the problem with you device?") 
split = problem.split(" ") 
keyList = [] 

def other(): 
     print("no solution") 

questionbank = [] 


with open ('111.txt') as questions: 
    for line in questions: 
     questionbank.append(line.strip().split(',')) 
# the following line are probably the source of the problem(up to calling the phone_problem function) 
solutions = {question[0]:question[1:] for question in questionbank} 


def phone_problem(): 
    while True: 
     for solution, key_words in solutions.items(): 
      if any(word in problem for word in key_words): 
       print(solution) 
       return 

phone_problem() 
if keyList == []: 
    with open("counter.txt", "r") as file: 
     for lines in file: 
       number = lines[0] 
    file.close() 
    text_file = open("help.txt", "w") 
    text_file.write(str(int(number)+1)) 
    text_file.write("\n{}      {}        {}         {}       {}".format(number,devType,brand,device,problem)) 
    text_file.close() 
    other() 
keyList = list(set(keyList)) 
for i in keyList: 
    print("Solution:",i) 
+3

111.txtからのサンプル入力を追加してください。 – ppasler

答えて

1

回答は、私は、コードにバグがあると思います...以下のコメントに

solutions = {} 
for i in questionbank: 
    # i = ['put your phone is rice', 'wet', 'damp', 'water', 'puddle'] 
    # i[-1] means last thing in list = 'puddle' 
    # i[0:len(i)-1] means everything in i except the last element 
    #    which could be rewritten as i[:-1] 
    solutions[i[-1]] = i[0:len(i)-1] 
    # solutions['puddle'] = ['put your phone is rice', 'wet', 'damp', 'water'] 

です。解決キーが行の最初の要素であるべきではありませんか?コードは次のように書かれています。

solutions = {question[0]:question[1:] for question in questionbank} 

def phone_problem(): 
    while True: 
     problem = input("Phone problem?") 
     for solution, key_words in solutions.items(): 
      if any(word in problem for word in key_words): 
       print(solution) 
       return 
+0

ありがとう、これは本当に私を助けます! – Joel

関連する問題