2016-12-10 4 views
-2

私は、誰かが自分の画面ことを答えるようにした場合の問題を持っています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'演算子は、一度に1単語しか動かない。たとえば、文2では ''壊れている 'を確認し、文2では' if '、文2では' screen 'のように文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

関連する問題