我最近發現了這個代碼,但一直在努力研究它是如何工作的。 「111.txt」是一個帶有列表行的文本文件,其中每行上的列表的第一部分是解決方案,該列表中的相應單詞是解決方案的關鍵詞。我瞭解除了第8行(solutions[i[-1]] ...
)之外的大部分內容。我一直在查找使用的不同模塊,但我仍然不明白該線路是什麼以及它是如何工作的。我對Python的使用很新,所以如果可能的話,我會非常感謝這一行的簡單解釋。提取文本文件中的第一個單詞,然後提取相應的單詞?
在此先感謝!
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()
「112.txt」 的例子:
put your phone is rice, wet, damp, water, puddle
replace you screen, screen, crack, smash, shatter...
更新: 我在你的代碼添加了,但它仍然簡化版,輸出的解決方案。無論我輸入什麼問題,它都會繼續運行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)
請從111.txt中添加示例輸入 – ppasler