2014-04-10 3 views
-2

(.. (위의 편집에 대응하여, 이것은 위의 링크에 나와 있지 않은 한 위의 질문은 내 의도 된 사용 무관하다))텍스트 게임 - 변환 소문자로 입력 텍스트 - 파이썬 3.0

내가 가진 문자열을 소문자로 변환하는 것과 비슷한 질문을 읽어보십시오. How to convert string to lowercase in Python

나는이 완벽하게 작동 방식을 이해

, 자신이 실패 이것 그러나 나의 시도.

다음은 디버그 블록에 대한 현재 설정 예입니다.

#Debug block - Used to toggle the display of variable data throughout the game for debug purposes. 
def debug(): 
    print("Would you like to play in Debug/Developer Mode?") 
    while True: 
     global egg 
     choice = input() 
     if choice == "yes": 
      devdebug = 1 
      break 
     elif choice == "Yes": 
      devdebug = 1 
      break 
     elif choice == "no": 
      devdebug = 0 
      break 
     elif choice == "No": 
      devdebug = 0 
      break 
     elif choice == "bunny": 
      print("Easter is Here!") 
      egg = 1 
      break 
     else: 
      print("Yes or No?") 
# 

그래서 다른 대문자 사용으로 미리 작성했습니다. 그러나 대문자로 2를 사용하는 대신 단어 당 하나의 if 문을 사용하고 싶습니다. 나는 다른 블록을 사용하여 True의 False 상태를 결정하는 아이디어가 있습니다.

def debugstate(): 
    while True: 
     global devstate 
     choice = input() 
     if choice == "Yes": 
      devstate = True 
      break 
     elif choice == "yes": 
      devstate = True 
      break 
     elif choice == "No": 
      devstate = False 
      break 
#Etc Etc Etc 

하지만 그냥 내가 이미
이 코드 행을, 다른 곳으로 이동할 것이 블록을 사용. 'Yes'가 아니면 else가 자동으로 devstate를 0으로 설정할 수 있지만 더 제어 된 환경을 선호한다는 것을 설정할 수 있음을 알고 있습니다. 나는 실수로 'yes'를 공백으로 입력하고 devmode를 해제하고 싶지 않습니다.

그래서 다시 질문에;

어떻게하면 다음과 같이 할 수 있습니까?

def debug(): 
    print("Debug Mode?") 
    while True: 
     global egg 
     choice = input() 
     if choice == "yes" or "Yes": 
      devdebug = 1 
      break 
     elif choice == "no" or "No": 
      devdebug = 0 
      break 
     elif choice == "egg": 
      devdebug = 0 
      egg = 1 
      print("Easter is Here") 
      break 
     else: 
      print("Yes or No?") 
# 

위의 코드는 아마도 가장 좋은 예는 아니지만 적어도 제가 난 단지 단어 당 하나의 if 문을 원하는 말할 때 가로 질러 포인트를 얻을 수 있습니다. (또한 xD에서 내 자신의 문제를 해결하지 않았 으면 좋겠다.)

그래서 어떻게 할 수 있습니까?

((또한 파이썬 포럼보다는 여기에 나오는 이유는 다른 사람이 다르게 사용 된 질문의 답을 함께 모으기보다는 내 자신의 방식으로 질문하는 것이 더 좋기 때문입니다.))

+2

_ "나는 이것이 어떻게 완벽하게 작동하는지 이해하지만,이 시도는 실패했습니다."_. 나는 당신이'.lower'를 사용하려고 시도한 코드를 보는데 흥미가있을 것이다. – Kevin

+1

그냥 choice.lower() == "yes"이면 사용 완료 – Retozi

+0

@Kevin 저를 혼란스럽게하는 .lower의 배치였습니다. "파이썬으로 문자열을 소문자로 변환하는 법"은 나를 위해 내게 외쳤다. .lower –

답변

6

(.lower 사용은) 당신은 몇 가지 변수 사용에 소문자 값을 넣을 수 있습니다 당신의 최선의 선택

choice = input() 
choice = choice.lower() 
if choice == 'yes': 
    dev_debug = 1 
    break 

또는

choice = input() 
if choice in ('yes', 'Yes'): 
    dev_debug = 1 
    break 
+0

첫 번째 예제를 시도했지만 완벽하게 작동했습니다! 두 번째 예를 들어 주셔서 감사합니다. 앞으로 확실히 사용할 제품입니다. 다시 한 번 감사 드리며 적절한 설정을 보여줍니다. –

+0

@GameWylder 두 번째 예제는 Wooble과 연결된 질문에 대해 허용 된 솔루션과 동일한 _exactly_입니다. –

2

'에서'사용하다 대신 원래 choice의 당신이 사건에 대해 신경 쓰지 않는 곳과 케이스가 중요한 위치를 여전히 choice를 사용할 수 있습니다

def debug(): 
    print("Debug Mode?") 
    while True: 
     global egg 
     choice = input() 
     lowchoice = choice.lower() 
     if lowchoice == "yes": 
      devdebug = 1 
      break 
     elif lowchoice == "no": 
      devdebug = 0 
      break 
     elif choice == "egg": 
      devdebug = 0 
      egg = 1 
      print("Easter is Here") 
      break 
     else: 
      print("Yes or No?") 
0

한 마디 대답 :

확인하려면 string.lower() 어떤 변수/메서드는 클래스/개체에 사용할 수 있습니다. dir (object/class/method())을 사용하십시오.

예를 들어

a="foo" 
type(a) 
<type 'str'> 
dir(a) 
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] 
관련 문제