2016-07-12 5 views
0

저는 최근에 Codecademy에서 파이썬을 배우기 시작했습니다. 그래서 저는 처음으로 실제 프로그램을위한 바위, 종이, 가위 게임을 만들기로 결정했습니다.while 회 돌이에서 if 문을 무시한 파이썬

프로그램을 테스트 할 때 Python은 while 루프 내에 중첩 된 큰 if/elif 문을 무시하는 것으로 보입니다. 게임은 임의의 정수 (0은 바위, 1은 종이, 2는 가위)를 생성 한 다음 (디버깅을 위해) 인쇄합니다. 그런 다음 플레이어에게 입력을 요구합니다. 그 후에 if 문을 사용하여 선택 사항을 평가하는 대신 플레이어에게 다른 선택을 요청합니다. 또한 새로운 무작위 정수를 출력하므로 if 문을 건너 뛰고 while 루프의 시작 부분으로 되돌아갑니다.

다음은 게임 코드입니다. if 문에 어떤 종류의 문법 오류가 있다면 나는 그것을 보지 않을 것이다. 아무도 무슨 일이 일어나는지 알아?

from random import randint 

def choose(): 
    print '\nWill you play rock, paper, or scissors?' 
    rawhumanchoice = raw_input('> ') 
    if rawhumanchoice == 'rock' or rawhumanchoice == 'r': 
     humanchoice = 0 
    elif rawhumanchoice == 'paper' or rawhumanchoice == 'p': 
     humanchoice = 1 
    elif rawhumanchoice == 'scissors' or rawhumanchoice == 's': 
     humanchoice = 2 
    else: 
     print '\nSorry, I didn\'t catch that.' 
     choose() 

def gameinit(): 
    roundsleft = 0 
    pcwins = 0 
    humanwins = 0 

    print 'How many rounds do you want to play?' 
    roundsleft = raw_input('> ') 

    while roundsleft > 0: 
     pcchoice = randint(0,2) 
     print pcchoice 

     humanchoice = -1 
     choose() 

     if humanchoice == 0: #This is what Python ignores 
      if pcchoice == 0: 
       print '\nRock and rock... it\'s a tie!' 
       roundsleft -= 1 
      elif pcchoice == 1: 
       print '\nPaper beats rock... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
      elif pcchoice == 2: 
       print '\nRock beats scissors... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
     elif humanchoice == 1: 
      if pcchoice == 0: 
       print '\nPaper beats rock... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
      elif pcchoice == 1: 
       print '\nPaper and paper... it\'s a tie!' 
       roundsleft -= 1 
      elif pcchoice == 2: 
       print '\nScissors beat paper... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
     elif humanchoice == 2: 
      if pcchoice == 0: 
       print '\nRock beats scissors... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
      elif pcchoice == 1: 
       print '\nPaper beats rock... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
      elif pcchoice == 2: 
       print '\nScissors and scissors... it\'s a tie!' 
       roundsleft -= 1 
    else: 
     if humanwins > pcwins: 
      result = 'The human wins the match!' 
     elif humanwins < pcwins: 
      result = 'The PC wins the match.' 
     elif humanwins == pcwins: 
      result = 'The match is a tie!' 

     print '\nThe score is %s:%s... %s \n' % (humanwins,pcwins,result) 
     gameinit() 

gameinit() 
+1

누군가가 [this] (http://stackoverflow.com/questions/32364127/store-function-result-into-variable)의 dup으로 닫을 수 있으면 감사 할 것입니다. – TigerhawkT3

+1

'choose' 함수가 변수를 수정하는 방법은 무엇입니까? –

+0

'humanchoice = -1' 행을주의 깊게 살펴보십시오. 코드는 if 문을 정확하게 무시합니다. –

답변

0

choose()로를 따라 잡고있다. 따라서 항상 None을 반환합니다. None은 결코 0과 같지 않습니다. 따라서 귀하의 if 문은 실행되지 않습니다.

파이썬 3에서 None0을 비교하는 것은 실제로는 오류가되므로 더 빨리 이해할 수 있습니다. 그러나 파이썬 2 에서조차도 choose()로 전화 한 후 겸손한 print humanchoice 문장을 사용하면 함수에서 None을 얻는 것이 아니라는 것을 빨리 알 수있었습니다.

choose() 끝에 return humanchoice을 추가하십시오.

+0

감사합니다. 나는 이것에 대해서 생각조차하지 않았다. – quantummechanic

0

당신은 선택 방법에서 돌아 누락하고 값을 반환하지 않습니다

humanchoice = choose() 
관련 문제