2017-02-24 17 views
-1

我正在製作一個程序,該程序是識別關鍵字的手機故障排除程序,然後根據所述關鍵字讀取並打印具有解決方案的文檔。現在用我的代碼,我問問題是什麼,如果我說液體,我想要液體文件打印和一系列與液體問題有關的問題提出。現在,當我這樣做時,它一直在問第一組問題,這是權力。任何人都可以幫助我,所以它會出現一系列問題嗎?獲取代碼以在一個變量下提問另一組問題

#Task 2 Trouble-Shooter 1.0 
#Zacharriah River Howells 
import time 
print('Hello and welcome to the mobile phone Trouble-Shooter!') 
time.sleep(2) 
problem = input('Please input what is wrong with your phone') 
if "power" in problem: 
    f = open('Power.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
if "battery" in problem: 
    f = open('Power.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
time.sleep(2) 

print('Have you charged your phone overnight? if not do so') 
time.sleep(1) 
powerans = input('Does your phone turn on now?') 
if powerans == 'yes': 
    f = open('Power.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[1]) 
    f.close() 
    exit() 

elif powerans == 'no': 
    f = open('Power.txt', 'r') 
    solution2 = f.readlines() 
    print(solution2[2]) 
    f.close() 
    exit() 

if "liquid" in problem: 
    f = open('Liquid.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
if "water" in problem: 
    f = open('Liquid.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
time.sleep(2) 

print('Have you let your phone dry in a container filled with rice?') 
time.sleep(1) 
liquidans = input('Does your phone turn on now?') 
if liquidans == 'yes': 
    f = open('Liquid.txt', 'r') 
    solution1 = f.readlines() 
    print(solution3[1]) 
    f.close() 
    exit() 

elif liquidans == 'no': 
    f = open('Liquid.txt', 'r') 
    solution2 = f.readlines() 
    print(solution4[2]) 
    f.close() 
    exit() 

if "software" in problem: 
    f = open('Software.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
if "apps" in problem: 
    f = open('Software.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
time.sleep(2) 

print('Have you tried reinstalling the app?') 
time.sleep(1) 
softeareans = input('Does your phone turn on now?') 
if softwareans == 'yes': 
    f = open('Software.txt', 'r') 
    solution1 = f.readlines() 
    print(solution5[1]) 
    f.close() 
    exit() 

elif softwareans == 'no': 
    f = open('Software.txt', 'r') 
    solution2 = f.readlines() 
    print(solution6[2]) 
    f.close() 
    exit() 

if "hardware" in problem: 
    f = open('Hardware.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
if "display" in problem: 
    f = open('Hardware.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
time.sleep(2) 

print('Have you tried installing the newest system update?') 
time.sleep(1) 
hardwareans = input('Does your phone turn on now?') 
if hardewareans == 'yes': 
    f = open('Hardware.txt', 'r') 
    solution1 = f.readlines() 
    print(solution7[1]) 
    f.close() 
    exit() 

elif hardwareans == 'no': 
    f = open('Hardware.txt', 'r') 
    solution2 = f.readlines() 
    print(solution8[2]) 
    f.close() 
    exit() 

if "unable" in problem: 
    f = open('Unidentifiable.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
if "dunno" in problem: 
    f = open('Unidentifiable.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
time.sleep(2) 
print('We cannot identify uour problem you can either') 
time.sleep(1) 
print('Visit our store') 
time.sleep(1) 
print('Visit our website') 

loop = input('Would you like to return to the start of the trouble-shooter?') 
if loop == 'yes': 
    problem() 
else: 
    exit() 
+0

告訴我們什麼是不工作。 – DyZ

+0

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在發佈您的MCVE代碼並準確描述問題之前,我們無法爲您提供有效的幫助。 – Prune

回答

0

你的問題都在一級縮進,所以他們都會被問到,不管用戶回答什麼。如果如你的問題所述,只有當用戶回答「液體」時才應該詢問某些問題,那麼這些問題應該在[if]語句中爲「液體」。例如:

if "power" in problem: 
    f = open('Power.txt', 'r') 
    solution1 = f.readlines() 
    print(solution1[0]) 
    f.close() 
    print('Have you charged your phone overnight? if not do so') 
    time.sleep(1) 
    powerans = input('Does your phone turn on now?') 

或者更好的是,在定義的函數中有問題,並在應用時調用這些函數。

編輯:請注意,如果問題之間存在依賴關係,最好使用不同的答案選項構建樹型數據結構,並鏈接到適用於任一答案的文件和答案。