저는 최근에 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()
누군가가 [this] (http://stackoverflow.com/questions/32364127/store-function-result-into-variable)의 dup으로 닫을 수 있으면 감사 할 것입니다. – TigerhawkT3
'choose' 함수가 변수를 수정하는 방법은 무엇입니까? –
'humanchoice = -1' 행을주의 깊게 살펴보십시오. 코드는 if 문을 정확하게 무시합니다. –