2016-12-10 27 views
-2

我現在試圖做一個計劃,將幫助這取決於他們在使用條件語句來查找字符串的話

型輸入我有一個問題,用戶修復他們的手機在那裏,如果有人回答說他們的屏幕沒有工作,它會打印解決方案的兩個答案。

import string 
punct = set(string.punctuation) 

sentence = input('Please enter what is wrong with your phone ').lower() 

sentence2 = ''.join(x for x in sentence if x not in punct) 

if 'smashed' and 'screen' in sentence2: 
    print ('Send your phone off for repairs') 

if 'screen' and 'not' and 'working' in sentence2: 
    print ('Charge your phone') 

這是我的錯誤:

+1

'in'運算符一次只能處理一個單詞。例如,你想檢查'sentence2'中是否有'smashed',然後如果'sentence2'中有'screen',就像'如果'在句子2中被「粉碎」,在句子2中是「screen」。 – Turix

+1

請複製代碼並將其粘貼到您的文章中,以便於我們對其進行調試 – techydesigner

+0

也請將該程序的輸出複製並粘貼到您的文章中。文字截圖幾乎是不合適的。 –

回答

0

要檢查一組關鍵詞都在一個句子中,你需要使用if word1 in sentence and word2 in sentence,或if all(...)

diagnosis = { 
    'Send your phone off for repairs': ['smashed', 'screen'], 
    'Charge your phone': ['screen', 'not', 'working'] 
} 
for solution, words in diagnosis.items(): 
    if all(word in sentence for word in words): 
     print(solution) 
+0

謝謝你工作了一個治療:) – theconcera2

相關問題