2017-02-20 9 views
0

こんにちは私はトラブルシューティングの質問プログラムで単語を見つけることについてここに質問があります。回答を出力する前にキーワードの質問をチェックするコンポーネントを追加するにはどうすればよいですか? Pythonのstringspythonで一致する単語

print ("Introduction Text") 
print ("Explanation of how to answer questions") 
Q1 = input ("Is your phone Android or Windows?") 
if Q1 == "yes": 
    print ("go to manufacturer") 
if Q1 == "no": 
    print ("next question") 
Q2 = input ("Is your screen cracked or broken?") 
if Q2 == "yes": 
    print ("Replace Screen") 
if Q1 == "no": 
    print ("next question") 
Q3 = input ("Does the handset volume turn up and down?") 
if Q1 == "no": 
    print ("replace Hardware") 
    print ("contact Manufacturer") 
if Q1 == "yes": 
    print ("next question") 
+0

キーワードは何ですか? – WhatsThePoint

答えて

0

あなたは文字列を検索できるようになるfindのようないくつかの便利なメソッドを持っています。さらに複雑な文字列検索を可能にするregular expressionライブラリもあります。ただし、サブ文字列検索を実行するには、inを実行してください。例として、あなたの最初の質問を取る、我々は、ユーザが「はい」と答えたことを確認し、電話の種類は、次のようなものを使用することにより、「アンドロイド」であるかどうかをすることができます

>>> answer = input("Is your phone Android or Windows?") 
Is your phone Android or Windows?"Yes android" 
>>> if "yes" in answer.lower(): 
...  if "android" in answer.lower(): 
...    print "What android..." 
... 
What android... 

あなたが持っていれば電話の種類(Windowsの、アンドロイド)のリストは、あなたはそのリストをループし、あなたの文字列内のアイテムのanyが存在しているかどうかを確認することができ、またはあなたはそれが非常に簡単になり、リストの内包表記を使用することができます

>>> answer = input("Is your phone Android or Windows?") 
Is your phone Android or Windows?"Yes, I've got a Windows and Android phone..." 
>>> matching = [s for s in phone_types if s in answer.lower()] 
>>> print matching 
['windows', 'android'] 

をあなたが追加したいものは、検索したいリストのようないくつかの事柄に依存します。したがって、あなたが実際に必要とするものに応じて、追加したいかもしれませんあなたの質問にいくつかのより多くの情報。

関連する問題