-1

This question is more of a "I SWEAR MY CODE SHOULD WORK"...

If you look at the section of my "Rock, Paper, Scissors" games I wrote while trying to learn the basics of python, there is a section that converts the user's input str to a number.

playerChoice = input('Rock, paper, scissors: ')
if playerChoice is ('Rock'or'rock'):
    playerChoiceNum = 0
    print ('PLAYER CHOICE IS ROCK')
if playerChoice is 'Paper' or 'paper':
    playerChoiceNum = 1
    print ('PLAYER CHOICE IS PAPER')
if playerChoice is 'Scissors' or 'scissors' or 'Scissor' or 'scissor':
    playerChoiceNum = 2
    print ('PLAYER CHOICE IS SCISSORS')

For some reason, python sets playerChoiceNum to '0' each time no matter what you type in as an input. I tried using == comparison operator's instead of 'is' to no avail.

Sometimes it decides to get stuck on playerChoiceNum = 2 and sometimes it is stuck on playerChoiceNum = 0.

I'm curious as to why this is happening as I feel it will teach me more about how python actually functions as language, but for the life of me cannot figure out what is going on. Any help would be appreciated!

Here is my full "game" code if you want to try running it for yourself.

from random import randint
playerScore = 0
while playerScore < 3:
    print('hey person, please choose rock, paper or scissors to try and beat the computer 3 times')
    playerChoice = input('Rock, paper, scissors: ')
    if playerChoice is ('Rock'or'rock'):
        playerChoiceNum = 0
        print ('PLAYER CHOICE IS ROCK')
    if playerChoice is 'Paper' or 'paper':
        playerChoiceNum = 1
        print ('PLAYER CHOICE IS PAPER')
    if playerChoice is 'Scissors' or 'scissors' or 'Scissor' or 'scissor':
        playerChoiceNum = 2
        print ('PLAYER CHOICE IS SCISSORS')

    computerChoiceNum = randint(0,2)

    if computerChoiceNum == 0:
        print('COMPUTER HAS CHOSEN ROCK')
    elif computerChoiceNum == 1:
        print('COMPUTER HAS CHOSEN PAPER')
    elif computerChoiceNum == 2:
        print('COMPUTER HAS CHOSEN SCISSORS')

    if playerChoiceNum == computerChoiceNum:
        print('DRAW')
    if playerChoiceNum == 0 and computerChoiceNum == 1:
        print('COMPUTER WINS')
    if playerChoiceNum == 0 and computerChoiceNum == 2:
        print('PLAYER WINS')
        playerScore+=1
    if playerChoiceNum == 1 and computerChoiceNum == 2:
        print('COMPUTER WINS')
    if playerChoiceNum == 1 and computerChoiceNum == 0:
        print('PLAYER WINS')
        playerScore+=1
    if playerChoiceNum == 2 and computerChoiceNum == 0:
        print('COMPUTER WINS')
    if playerChoiceNum == 2 and computerChoiceNum == 1:
        print('PLAYER WINS')
        playerScore+=1
    print('DEBUG INFO:\n'
          'Player Choice: ' + str(playerChoice)+'\n' +
          'Player Choice Number Conversion: ' +   str(playerChoiceNum)+'\n' +
          'Computer Choice: ' + str(computerChoiceNum)+'\n' +
          'Player Score: ' + str(playerScore)+'\n')
Mark D.
  • 1
  • 1
  • This seems like a common rookie error, but you can not be faulted for it as a beginner since it is hard to know what to search for off hand. However, you should read the duplicate question and answers carefully. Python is working fine, but its logical operators do not work as you currently think they do. – Mad Physicist Apr 04 '16 at 03:46
  • Yeah, it's hard to find out if the question has been previously asked without really knowing the how and why of what you're trying to accomplish. Thanks for pointing me in the right direction, many thanks! – Mark D. Apr 04 '16 at 04:07

3 Answers3

3

This line

if playerChoice is ('Rock'or'rock'):

does not check if playerChoice is equal to one of two strings. It simply checks if the name playerChoice refers to the same object as the string literal 'Rock'. What you want is

if playerChoice in ('Rock', 'rock'):

etc.

chepner
  • 497,756
  • 71
  • 530
  • 681
3

This part is not doing what you think it does:

if playerChoice is ('Rock'or'rock'):
    playerChoiceNum = 0
    print('PLAYER CHOICE IS ROCK')
if playerChoice is 'Paper' or 'paper':
    playerChoiceNum = 1
    print('PLAYER CHOICE IS PAPER')
if playerChoice is 'Scissors' or 'scissors' or 'Scissor' or 'scissor':
    playerChoiceNum = 2
    print('PLAYER CHOICE IS SCISSORS')

is compares if two objects are the same object, not if they are equal.

or cannot be used the way you do.

I suggest you try the following:

if playerChoice == 'Rock' or playerChoice == 'rock':
    playerChoiceNum = 0
    print('PLAYER CHOICE IS ROCK')
if playerChoice == 'Paper' or playerChoice == 'paper':
    playerChoiceNum = 1
    print('PLAYER CHOICE IS PAPER')
if playerChoice == 'Scissors' or playerChoice == 'scissors' or playerChoice == 'Scissor' or playerChoice == 'scissor':
    playerChoiceNum = 2
    print('PLAYER CHOICE IS SCISSORS')

Alternatively, you could use in list or in Tuple:

if playerChoice in ['Rock', 'rock']:
    playerChoiceNum = 0
    print('PLAYER CHOICE IS ROCK')
etc...

Or lower case your input:

if playerChoice.lower() == 'rock':
    playerChoiceNum = 0
    print('PLAYER CHOICE IS ROCK')
etc...

Your next step in your learning will maybe to use dictionaries as suggested by @idjaw

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • thanks man, appreciate the thorough answer. Will definitely work on dictionaries, was trying to do a "finish this in 15 minutes" type challenge and clearly didn't go for efficiency haha! For lines 9-11, was I literally asking "If playerchoice is equal to paper is true OR if 'paper' equals true; then do this"? I see now the error in my logic if so.. – Mark D. Apr 04 '16 at 04:10
3

That is because your conditional checks are not doing what you think they are doing.

if playerChoice is ('Rock'or'rock')

If you break that down, this is what is happening:

This: ('Rock' or 'rock')

Will evaluate to 'Rock' and then is will actually compare with playerChoice to determine if they are of the same object as the string 'Rock'.

You want to use

if playerChoice in ('Rock', 'rock')

Furthermore, you really should refactor that to use something like a dictionary to handle your checks:

choices = {
    "rock": 0,
    "paper": 1,
    "scissors": 2
}

print("Player choice is {}".format(choices.get(playerChoice.lower(), "an invalid entry")))

So, what happened there is that everything is being kept under one case setting (lower case). So, your input held inside playerChoice will be set to lowercase via the lower() method, and then from there it's a simple dictionary look-up to get the appropriate number.

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • Thanks for your answer man, will make sure to use a dictionary from now on to handle my checks. Also, thanks for the showing me the ".lower()" function, that will definitely come in handy in the future! – Mark D. Apr 04 '16 at 04:14
  • @MarkD. you are very welcome! Good luck. – idjaw Apr 04 '16 at 04:19