2017-02-20 2 views
0

안녕하세요 문제 해결 질문 프로그램에서 단어를 찾는 것에 대해 질문이 있습니다. 어떻게 답변을 출력하기 전에 핵심어에 대한 질문을 확인할 구성 요소를 추가 할 수 있습니까? 파이썬 strings파이썬에서 일치하는 단어

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... 

당신이있어 경우 : 예를 들어 당신에게 첫 번째 질문을 가지고 가서, 우리는 다음과 같은 것을 사용하여 사용자가 전화 유형이 "안드로이드"이며 "예"하고 있는지 여부를 응답 한 확인하실 수 있습니다 전화 유형 (윈도우, 안드로이드)의 목록은 해당 목록을 통해 반복하고 문자열의 항목의 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'] 

을 추가하려는 항목은 검색하려는 목록과 같은 몇 가지 항목에 따라 달라집니다. 실제로 필요한 항목에 따라 추가 할 항목이있을 수 있습니다. 당신의 질문에 대한 좀 더 많은 정보.