ValueError: 'SSS' is not in list


#1

Ran this a couple of times, have optimized and cleaned the code. Error seems to be around line 28, but I can't see it. Have looked at 5 other examples from other students to get an idea of what they did.

When testing input: "R", "S", "P", it works as advertised, but fails on "garbage input"

Will probably be some silly little thing that I missed.

ERROR

Traceback (most recent call last):
  File "python", line 74, in <module>
  File "python", line 72, in play_RPS
  File "python", line 28, in decide_winner
ValueError: 'SSS' is not in list

CODE

# ROCK-PAPPER-SCISSORS
# The program should does the following:
# Prompt the user to select either Rock, Paper, or Scissors
# Instruct the computer to randomly select either Rock, Paper, or Scissors
# Compare the user's choice and the computer's choice
# Determine a winner (the user or the computer)
# Inform the user who the winner is

from random import randint
from time import sleep

# Creates options for rock, paper, scissors
options = ["R", "P", "S"]

LOSS_MESSAGE = "Buhaha, You lost! The computer Rules!"
WIN_MESSAGE = "You won! Yippee!"

def decide_winner(user_choice, computer_choice):
    
    # spits out what user and computer selected
    print "You selected: %s" % user_choice
    print "Computer selecting..."
    sleep(1)
    print
    print "Computer selected: %s" % computer_choice
    
    # links user choice to index
    user_choice_index = options.index(user_choice)
    computer_choice_index = options.index(computer_choice)
    
    # Tie
    if user_choice_index == computer_choice_index:
        print "It's a tie!"
        
    # Rock beats Scissors    
    elif user_choice_index == 0 and computer_choice_index == 2:
        print WIN_MESSAGE
        
    # Paper beats Rock    
    elif user_choice_index == 1 and computer_choice_index == 0:
        print WIN_MESSAGE
        
    # Scissors beat Paper    
    elif user_choice_index == 2 and computer_choice_index == 1:
        print WIN_MESSAGE
    
    # Bad input
    elif user_choice_index > 2:
        print "Incorrect choice, exiting"
        return
    
    # Computer wins
    else:
        print LOSS_MESSAGE		          
		                
def play_RPS():
    
    # Creates a function to play the game
    print "Let's play Rock-Paper-Scissors!"
    print
    sleep(1)
    
    # Gets the user choice
    user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ")
    user_choice = user_choice.upper()
    print
  	
  	# Gets the computer's choice
    computer_choice = options[randint(0,len(options)-1)]
    
    # Calls function to determine winner
    decide_winner(user_choice, computer_choice)

play_RPS()

#2

here:

user_choice_index = options.index(user_choice)

when the entry is not in list, it will thrown an error, the suggested fix by the exercise:

    elif user_choice_index > 2:
        print "Incorrect choice, exiting"
        return

is just crap, it doesn't work.

You could check if the user choice is in list:

if user_choice in options:
    user_choice_index = options.index(user_choice)
else:
    print "not a valid choice"

#3

Hi Stetim94,

Thanks for the input. Was wondering if I messed up by not following something correctly, so I had a look at the tutorial,

Updated the code, it is working now. Thank you!

    # links user choice to index
    if user_choice in options:
        user_choice_index = options.index(user_choice)
        computer_choice_index = options.index(computer_choice)
    else:
        print "Incorrect choice, exiting"
        return

Complete script.

# ROCK-PAPPER-SCISSORS
# The program should does the following:
# Prompt the user to select either Rock, Paper, or Scissors
# Instruct the computer to randomly select either Rock, Paper, or Scissors
# Compare the user's choice and the computer's choice
# Determine a winner (the user or the computer)
# Inform the user who the winner is

from random import randint
from time import sleep

# Creates options for rock, paper, scissors
options = ["R", "P", "S"]

LOSS_MESSAGE = "Buhaha, You lost! The computer Rules!"
WIN_MESSAGE = "You won! Yippee!"

def decide_winner(user_choice, computer_choice):
    
    # spits out what user and computer selected
    print "You selected: %s" % user_choice
    print "Computer selecting..."
    sleep(1)
    print
    print "Computer selected: %s" % computer_choice
    
    # links user choice to index
    if user_choice in options:
        user_choice_index = options.index(user_choice)
        computer_choice_index = options.index(computer_choice)
    else:
        print "Incorrect choice, exiting"
        return
        
    # Tie
    if user_choice_index == computer_choice_index:
        print "It's a tie!"
        
    # Rock beats Scissors    
    elif user_choice_index == 0 and computer_choice_index == 2:
        print WIN_MESSAGE
        
    # Paper beats Rock    
    elif user_choice_index == 1 and computer_choice_index == 0:
        print WIN_MESSAGE
        
    # Scissors beat Paper    
    elif user_choice_index == 2 and computer_choice_index == 1:
        print WIN_MESSAGE
    
    # Computer wins
    else:
        print LOSS_MESSAGE		          
		                
def play_RPS():
    
    # Creates a function to play the game
    print "Let's play Rock-Paper-Scissors!"
    print
    sleep(1)
    
    # Gets the user choice
    user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ")
    user_choice = user_choice.upper()
    print
  	
  	# Gets the computer's choice
    computer_choice = options[randint(0,len(options)-1)]
    
    # Calls function to determine winner
    decide_winner(user_choice, computer_choice)

play_RPS()

#4

let me express myself more clearly, the instructions by the exercise are just plain wrong (about handling invalid input), yes, the instruction say to code:

    elif user_choice_index > 2:
        print "Incorrect choice, exiting"
        return

but it will simply have no effect


#5

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