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.
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
# 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()