RPS.py - Step 18 Question


I don't understand how a user could select a value greater than an index of 2 when the options are set to only have a total of 3 selections; thus impossible to have a larger index. Can someone please explain to me how this could happen, and why we're putting in:

elif user_choice_index > 2:
print 'That\'s an invalid selection.'

or something to that effect?

What's the best way, in your opinion, to actually check what the user selected against the available options in the options list?

Otherwise, the code works as intended, but I can't figure out what I'm missing that's giving me the following errors, other than the obvious that it's not in the defined list:

Traceback (most recent call last):
File "RPS.py", line 43, in
File "RPS.py", line 41, in play_RPS
decide_winner(user_choice, computer_choice)
File "RPS.py", line 16, in decide_winner
user_choice_index = options.index(user_choice)

Am I getting ahead of myself? Is there an error in my code?

Thank you very much for your time!

'''This is a simple game of Rock, Paper, Scissors,
otherwise known as ROSHAMBO'''

from random import randint
from time import sleep

options = ['R', 'P', 'S']
LOSE = 'You lost!'
WIN = 'You won!'

def decide_winner(user_choice, computer_choice):
  print 'This round, you selected: %s!' % user_choice
  print 'Computer selecting...'
  print '%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! That\'s no fun, try again!'
    return play_RPS()
  elif user_choice_index == 0 and computer_choice_index == 2:
    print WIN
  elif user_choice_index == 1 and computer_choice_index == 0:
    print WIN
  elif user_choice_index == 2 and computer_choice_index == 1:
    print WIN
  elif user_choice_index > 2:
    print 'That\'s an invalid selection. Try again.'
    print LOSE

def play_RPS():
  print 'Welcome to ROSHAMBO, otherwise known as Rock, Paper, Scissors!'
  user_choice = raw_input(
    'Select \'R\' for rock, \'P\' for paper, and \'S\' for scissors: ')
  user_choice = user_choice.upper()
  computer_choice = options[randint(0,len(options)-1)]
  decide_winner(user_choice, computer_choice)


Question about the exercise Rock Paper Scissors

i had a lengthy discussion with this about someone, it doesn't work. As you can read here

the approach suggested by the exercise to catch invalid input is just plain crap, it doesn't work


Thank you very much! I was up for far too long last night trying to figure that out, coming up empty! Much appreciated!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.