I recently finished learning Python on Codecademy, so I decided to make a rock, paper, scissors game for my first real program since it seemed fairly simple.
When testing the program, Python seems to ignore the big if/elif statement that's nested within the while loop. The game generates a random integer (0 being rock, 1 being paper, 2 being scissors) and then prints it (for debugging). Then it prompts the player for input. After that, instead of evaluating the choice with the if statement, it just asks the player for another choice. It also prints a new random integer so I know it's just skipping over the if statement and going back to the beginning of the while loop.
Below is the code for the game. If there's some kind of syntax error with the if statement I'm not seeing it. Does anyone know what's going on?
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()