2014-12-14 3 views
0

이 Python3 코드 (단축)를 향상 시켜서 GreeTinGs라는 단어를 찾을 수 있습니다 (대소 문자 일치하지 않음). 내가 사용할 데이터는 대소 문자 혼란입니다. 나는 또한 사용자가 대문자와 소문자를 포함 할 수있는 단어를 입력 할 수있게하고 싶다. 데이터를 모두 위 또는 아래로 변환 할 수는 없습니다. Ref : 'HeLLo'는 찾을 수 있지만 'GreeTinGs'는 찾을 수 없습니다.입력, 파이썬 3에서 대문자 소문자를 찾습니다.

현재 "casefold"명령은 대부분의 용도에 적합합니다. 데이터에는 단어 중간에 대문자가 있습니다. casefold 같은 다른 명령이나 내가 사용할 수있는 다른 명령이 있습니까? ref를 위해 또 다른 입력이있을 것입니다. 대소 문자를 구분하는 검색이므로이 옵션을 사용해야합니다. 여기

코드이며 모든 도움에 감사드립니다 :

import random 

greetings = ['hola', 'hello', 'GreeTinGs', 'hi', 'Hi', 'hey!','hey'] 
question = ['How are you?','How are you doing?'] 
responses = ['Okay',"I'm fine"] 

while True: 
     userInput = input(">WHAT:").casefold() 
     if userInput in greetings: 
      greetingsrandom = random.choice(greetings) 
      print (greetingsrandom) 
     elif userInput in question: 
      responsesrandom = random.choice(responses) 
      print (responsesrandom) 
     else: 
      print("I did not understand what you said") 
+1

할 수 있을까? –

+0

왜'str.casefold()'를 사용할 수 있습니까? 그러나 lower() 나 upper()는 사용할 수 없습니까? 이것은 완전히 임의적 인 제한이며 여기에 그러한 제한이있는 경우 casefold()가 허용되지 않을 수도 있습니다 *. *. –

+0

입니다. 프로그래밍 코드, 즉 프레임 워크 용 도움말 파일이 포함될 것이기 때문입니다. 단어 중간에 틀린 경우를 입력하면 검색 결과를 가져오고 싶습니다. 그러나, 나는 시스템이 잘못된 대소 문자를 표시하는 것을 원하지 않습니다. – user4358395

답변

1

는 소문자 구분을 위해 당신이 왜 입력이 모두 이하로 변환 할 수없는 모두 대문자

import random 

greetings = ['hola', 'hello', 'GreeTinGs', 'hi', 'Hi', 'hey!','hey'] 
question = ['How are you?','How are you doing?'] 
responses = ['Okay',"I'm fine"] 

while True: 
     userInput = input(">WHAT:").casefold() 
     if userInput.upper() in [x.upper() for x in greetings]: 
      greetingsrandom = random.choice(greetings) 
      print (greetingsrandom) 
     elif userInput in [x.upper() for x in question]: 
      responsesrandom = random.choice(responses) 
      print (responsesrandom) 
     else: 
      print("I did not understand what you said") 
+0

감사합니다. 잘 작동합니다. – user4358395

관련 문제