2016-02-26 41 views
0

所以我試圖做一個簡單的故障排除應用程序的手機和問題的解決方案,約5分鐘,我不能通過第4行時,用戶進入手機型號,它只是停止而不是繼續對話。我不知道爲什麼。任何幫助深表感謝。同樣在第6行,我很喜歡這樣做,以便當用戶寫出關於手機有什麼問題的文本時,它會選擇「破碎」,「破解」,「溼」等字樣,但我沒有任何線索如何再次,任何幫助,非常感謝!Python:打破手機故障排除應用程序

brand = raw_input("Please state your phone brand. ") 
if brand.lower() == ("iphone"): 
    iphone = raw_input ("Please state the model. ") 
    if iphone.lower() == ("2G"): 
     iproblem2g = raw_input ("Please state your problem. ") 
     if iproblem2g.lower() == ("broken") or ("broke"): 
      ibroke = raw_input ("Is the hardware broken or the software? ") 
      if ibroke.lower() == ("hardware") or ("hard"): 
       print ("...") 
+0

您需要縮進第2行。 –

+0

@pp_我認爲你的意思是第3行。就我的經驗而言,文本編輯並不是從0開始。:) – zondo

+0

雖然這個問題與另一個問題有關係,但它不是重複的,而是超級集合。 這段代碼有很多問題可以解決。 – Xexeo

回答

2

有幾個問題。試試這個:

brand = raw_input("Please state your phone brand. ") 
if brand.lower() == ("iphone"): 
    iphone = raw_input ("Please state the model. ") 
    if iphone.lower() == ("2g"): 
     iproblem2g = raw_input ("Please state your problem. ") 
     if iproblem2g.lower() in ("broken", "broke"): 
      ibroke = raw_input ("Is the hardware broken or the software? ") 
      if ibroke.lower() in ("hardware", "hard"): 
       print ("...") 

縮進在Python中非常重要。而且,由於輸入值被小寫,「2G」從來沒有匹配過輸入值。最後,匹配多個值在列表或元組中更簡單,並且不會像以前那樣工作(它總會返回True)。

爲什麼總是爲真?以if iproblem2g.lower() == ("broken") or ("broke"):爲例。這是檢查iproblem2g.lower() == ("broken")("broke")("broke")將始終爲真,因爲它被認爲是Truthy value in Python因此整個條件將始終爲真。

乾杯!

0

此行可能是語義錯誤的(即,它編譯但你希望不工作):

iproblem2g.lower() == ("broken") or ("broke") 

你實際上是問如果這意味着

(iproblem2g.lower() == ("broken")) or True 

(iproblem2g.lower() == ("broken")) or ("broke") 

而且這將永遠是真的

你不能用這種方法檢查是否有「==」兩件事。如果你想要做這種問題(是== B或一個== c)您應使用:

(iproblem2g.lower() == ("broken")) or (iproblem2g.lower() == ("broke")) 

,或者更Pythonesc

iproblem2g.lower() in ["broken","broke"] 

然而,即使這個問題不是一個好的選擇,因爲它只會匹配完美的單詞「破碎」或「破碎」,而不是包含它的文本。這可能會更好地問:

"broken" in iproblem2g.lower() or "broke" in iproblem2g.lower() 

這樣你會發現在任何句子中的單詞。

你可以把它更好地創建一個簡單的函數來檢查,如果任何一組單詞的是在一個句子:

def check(words,sentence): 
    for word in words: 
     if word in sentence: 
      return True 
    return False 

可以使用.lower(),甚至做一些預處理。 trim()

最後,還有一種小竅門,你沒有注意到:如果「破」是字符串,「破」總是(因爲它是一個子字符串),所以你不需要檢查兩者。

+0

他沒有注意到,因爲他沒有在找。他正在考慮不區分大小寫的平等。 – zondo