0

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()
  • 1
    If someone could close as a dup of [this](http://stackoverflow.com/questions/32364127/store-function-result-into-variable) I would be grateful. – TigerhawkT3 Jul 12 '16 at 05:47
  • 1
    How should the `choose` function modifiy a variable, that isn't in its scope? –  Jul 12 '16 at 05:48
  • Carefully look at the line `humanchoice = -1`... The code correctly ignores that if statement – OneCricketeer Jul 12 '16 at 05:49
  • Also, use while loops **or** recursion (calling a function within itself), not both. For beginning purposes, stick to the while loop – OneCricketeer Jul 12 '16 at 05:51
  • The `humanchoice` variable that you have defined in `gameinit()` is different than the one defined in `choose()` (even though they are named the same) This is because functions do not share variables. To get the value out of a function, you should use the `return` statement. – Burhan Khalid Jul 12 '16 at 05:52

2 Answers2

0

you are missing to return from method choose, and catching it with

humanchoice = choose()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kevin
  • 901
  • 1
  • 7
  • 15
0

choose() does not return a value. Therefore it always returns None. None will never be equal to 0. Therefore your if statement is never triggered.

In Python 3 comparing None and 0 would actually be an error, which would help you figure this out more quickly. However, even in Python 2, a humble print humanchoice statement after your call to choose() would have quickly revealed that you weren't getting anything but None from your function.

Add return humanchoice at the end of choose().

kindall
  • 178,883
  • 35
  • 278
  • 309