Python : Rock, Paper, Scissors (Error with computer input)


#1



Python : Rock Paper Scissors

$ RPS.py
bash: RPS.py: command not found
$ python RPS.py
You are playing a Rock, Paper, Scissors simulation.
Select R for rock, P for Paper, or S for scissors

r
You chose: R
Computer selecting...
Computer chose: [1]
Traceback (most recent call last):
File "RPS.py", line 44, in
play_RPS()
File "RPS.py", line 42, in play_RPS
decide_winner(user_choice, computer_choice)
File "RPS.py", line 19, in decide_winner
computer_choice_index = options.index(computer_ch
oice)
ValueError: [1] is not in list
$


I was expecting that value that is generated for the computer player would match up with the "R", "P", or "S" "options" list. I have a feeling that i am using .index the wrong way and its not converting the (int) to its coordinating (str) char in the list.


# Rock
# Paper
# Scissors

from random import randint
from time import sleep

options = ["R", "P", "S"]

you_lost = "You lost."
you_won = "You win. Computer loses"

def decide_winner(user_choice, computer_choice):
  print "You chose: %s" % user_choice
  print "Computer selecting..."
  sleep(1)
  print "Computer chose: %s" % computer_choice
  user_choice_index = options.index(user_choice)
  computer_choice_index = options.index(computer_choice)
  
  if user_choice_index == computer_choice_index:
    print "It's a tie."
  elif user_choice_index == [0] and computer_choice_index == [2]:
    print you_won
  elif user_choice_index == [2] and computer_choice_index == [1]:
    print you_won
  elif user_choice_index == [1] and computer_choice_index == [0]:
    print you_won
  elif user_choice_index > [2]:
    print "Invalid choice."
  else:
    print you_lost
    
  return

def play_RPS():
  print "You are playing a Rock, Paper, Scissors simulation."
  user_choice = raw_input("Select R for rock, P for Paper, or S for scissors\n> ")
  sleep(1)
  user_choice = user_choice.upper()
  computer_choice = [randint(0, len(options)-1)]
  decide_winner(user_choice, computer_choice)

play_RPS()


#2

multiply things, if you want to execute a python script in the current directory, do:

./pythonscriptname.py

do make sure it is executabe (chmod u+x pythonscriptname.py). Uhm... this line:

computer_choice = [randint(0, len(options)-1)]

why not simple do:

computer_choice_index = randint(0, len(options)-1)

this will directly give you the index. And then if you want to print it, simple use the index to access the options and show the computer choice:

print "Computer chose: %s" % options[computer_choice_index]

#3

@arraymaster55454: Just to complement what @stetim94 said, here's a link with an explanation about the index() function and remember that doing:

variable = randint(0, len(options) -1) 
variable = [randint(0, len(options) -1)]

Are two different things, one will be a number and the other will be a list, containing just one element.

Python List index


#4

I am a bit hasty, again, if you want to use ./pythonscript.py i think you need to put this:

#!/usr/bin/env python

in the first line, this will tell the interpreter to use python (assuming linux system), if you want to execute it anywhere, put it in your $PATH


#5

Thank you. Well said.

Also noticed I initially did not declare the list needed to search within. Seen here.

Incorrect:

def play_RPS():
  print "You are playing a Rock, Paper, Scissors simulation."
  user_choice = raw_input("Select R for rock, P for Paper, or S for scissors\n> ")
  sleep(1)
  user_choice = user_choice.upper()
  computer_choice = [randint(0, len(options)-1)]
  decide_winner(user_choice, computer_choice)

Correct:

computer_choice = options[randint(0, len(options)-1)]

Full updated code:

# Rock
# Paper
# Scissors

from random import randint
from time import sleep

options = ["R", "P", "S"]

you_lost = "You lost."
you_won = "You win. Computer loses"

def decide_winner(user_choice, computer_choice):
  print "You slected: %s" % user_choice
  print "Computer selecting..."
  sleep(1)
  print "Computer chose: %s" % computer_choice
  
  user_choice_index = options.index(user_choice)
  computer_choice_index = options.index(computer_choice)
  
  if user_choice_index == computer_choice_index:
    print "It's a tie."
  elif user_choice_index == 0 and computer_choice_index == 2:
    print you_won
  elif user_choice_index == 2 and computer_choice_index == 1:
    print you_won
  elif user_choice_index == 1 and computer_choice_index == 0:
    print you_won
  elif user_choice_index > 2:
    print "Invalid choice."
  else:
    print you_lost
    
  return

def play_RPS():
  print "You are playing a Rock, Paper, Scissors simulation."
  user_choice = raw_input("Select R for rock, P for Paper, or S for scissors\n> ")
  sleep(1)
  user_choice = user_choice.upper()
  computer_choice = options[randint(0, len(options)-1)]
  decide_winner(user_choice, computer_choice)

play_RPS()

#6