The instruction for step 18 says the following:
"What if the user’s choice has an index greater than 2? That’s garbage! Add one more elif statement that checks for this condition.
Inside of the elif block, print a message to the user that indicates that an invalid option was selected. On the next line, use return to exit the block."
Now, why would the index ever be greater than 2? This is the first question.
The second problem, is that the instructions say to use the following command:
user_choice_index = options.index(user_choice)
But what if the user_choice is something other than R, P, or S? Then I get an errror telling me that my choice is not on the list (ValueError).
I got around the problem by appending the user choice to my list of options (options = [“R”, “P”, “S”]), since even if they input an R, P, or S, the index will always grab the first one. Also, this ensure that a ‘wrong’ answer will ALWAYS have an index greater than 2.
Yay, problem solving, great! But, I assume that if I’m following the instructions as written, then I should get a program that works as described. Am I missing something?
Thanks for any insight!
""" This is a rock/paper/scissors simulator. The user will select either rock, paper, or scissors, then the computer will randomly generate one of the three. Then a decision will be made based on who wins, and the user will be informed.""" from random import randint from time import sleep options = ["R", "P", "S"] LOSE_STRING = "You lost! Should have chosen rock!" WIN_STRING = "You won! Still should have chosen rock." def decide_winner(user_choice,computer_choice): print "You have chosen %s" % (user_choice) options.append(user_choice) print "Computer selecting..." sleep(1) print "The computer has chosen %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 "You have tied! Should have chosen rock. Maybe you did, I don't know. I'm just a dumb computer" elif user_choice_index == 0 and computer_choice_index == 2: print WIN_STRING elif user_choice_index == 1 and computer_choice_index == 0: print WIN_STRING elif user_choice_index == 2 and computer_choice_index == 1: print WIN_STRING elif user_choice_index > 2: print "Invalid" return else: print LOSE_STRING def play_RPS(): print "Welcome to the RPS simulator! Can you outsmart randomness?" user_choice = raw_input("Input R for rock, P for Paper, or S for Scissors: ").upper() sleep(1) computer_choice = options[randint(0,len(options)-1)] decide_winner(user_choice,computer_choice) play_RPS()