2014-09-16 2 views
0

raw_input을 실험하고 있는데 내 첫 번째 if 문과 내 else 문을 제외하고는 정상적으로 작동합니다. 코드를 실행하고 제 2 문항에 raw_input을 입력하면 (if 문에 'League'뒤에 나열), 첫 번째 if와 두 번째 질문 모두를 반환합니다. 첫 번째 문언은 if 인 경우에만 인쇄됩니다. 어떤 생각이 잘못된거야?여러 if 문이있는 조건문 (raw_input 포함)

name = raw_input('What is your name?\n') 
game = raw_input('\nWhat MOBA do you play/have played? \n(The answer ' \ 
    'must be case sensitive i.e. LoL for League and DoTA for y\'know the whole darn thing '\ 
    'is too '\ 
    'darn long to say even though I typed a lot more than the name)\n') 

if game in ('League' , 'League of Legends' , 'LoL'): 
    print '\nYou go girl! ' 
if game in ('DoTA' , 'DoTA 2' , 'DoTA2'): 
    print '\nThat\'s cool and all but........ Go play League you dang noob. ' 
else: 
    print '\nAre you kidding me? You play %s? I\'m severely disappointed in you %s. You ' \ 
     'should at least be playing ' \ 
     'one of these popular games. \nShame, shame, shame. Go install one. ' % (game, name) 
+0

두 번째 'if'대신 ' elif' – Kamehameha

답변

0

, 당신의 들여 쓰기는 또한 조금 엉망이되면 중첩 된 조건문은 중첩 된 조건문 만 실행합니다. 첫 번째 문이 참이면 사용자에게 string.lower() 메서드를 사용하여 사용자에게 지시하지 않아도됩니다. 대/소문자를 구분하는 입력을 사용하고 변수를 사용하여 이러한 목록을 저장하면 코드의 다른 부분에서 다시 사용할 수 있습니다.

name = raw_input('What is your name?\n') 
game = (raw_input('\nWhat MOBA do you play/have played?')).lower() 

awesomeGames=['league' , 'league of legends' , 'lol'] 
coolGames=['dota' , 'dota 2' , 'dota2'] 
if game in awesomeGames: 
    print '\nYou go girl! ' 
elif game in coolGames: 
    print '\nThat\'s cool and all but........ Go play League you dang noob. ' 
else: 
    print '\nAre you kidding me? You play %s? I\'m severely disappointed in you %s. You ' \ 
    'should at least be playing ' \ 
    'one of these popular games. \nShame, shame, shame. Go install one. ' % (game, name) 
+0

설명해 주셔서 감사합니다. 대소 문자를 구별하지 않는 것이 더 합리적이며 코드는 elif로 더 잘 작동합니다. 나는 항상 엘프를 혼란에 빠뜨린다. 의견을 보내 주셔서 감사합니다. –

0

들여 쓰기 레벨이 잘못되었습니다. 두 번째 if 문에 대해 더 많은 공간을 사용하십시오. 또는 두 번째 대신에 1 공간 및 elif가 적습니다.

는 (입력이 인스턴스에서 두 번째 목록에있는 경우) 및 else 입력이이 목록에 없을 경우 당신은 항상 다른 조건문의 수에 대한 첫 번째 조건에 대한 if, elif을 사용할 수 있습니다
0

두 번째 if 상태를 elif으로 바꿉니다. elif 조건이 일치 할 때까지 원하는만큼 여러 번 사용할 수 있지만 마지막 조건은 else (다른 조건이 실패 할 때 기본 명령문과 같습니다)