Condition for "Invalid option!" doesn't work


#1


When an invalid letter or number entered, I'm getting the error bellow instead of this message "Invalid option!"

Rock, Paper, or Scissor ?
Select 'R' for Rock, 'P' for Paper, or 'S' for Scissors: f
Your choice is: F
Computer selecting...
Computer choice is: P
Traceback (most recent call last):
File "RPS.py", line 47, in
play_RPS()
File "RPS.py", line 45, in play_RPS
decide_winner(user_choice, computer_choice)
File "RPS.py", line 22, in decide_winner
user_choice_index = options.index(user_choice)
ValueError: 'F' is not in list


It should execute the below code and print on console "invalid option!" when any letter or number except R(r) or P(p) or S(s) letters entered.

elif user_choice_index > 2:
    print "Invalid option!"
    return


from random import randint
from time import sleep

options = ["R", "P", "S"]
LOSE_MASSAGE = "You lost!"
WIN_MASSAGE = "You won!"

def decide_winner(user_choice, computer_choice):
  print "Your choice is: %s" % user_choice
  print "Computer selecting... "
  sleep(1)
  print "Computer choice is: %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!"
    return play_RPS()
  elif user_choice_index == 0 and computer_choice_index == 2:
    print WIN_MASSAGE
  elif user_choice_index == 1 and computer_choice_index == 0:
    print WIN_MASSAGE
  elif user_choice_index == 2 and computer_choice_index == 1:
    print WIN_MASSAGE
  elif user_choice_index > 2:
    print "Invalid option!"
    return
  else:
    print LOSE_MASSAGE
  
def play_RPS():
  print "Rock, Paper, or Scissor?"
  user_choice = raw_input("Select 'R' for Rock, 'P' for Paper, or 'S' for Scissor: ")
  sleep(1)
  user_choice = user_choice.upper()
  computer_choice = options[randint(0,len(options)-1)]
  decide_winner(user_choice, computer_choice)

play_RPS()

Actually, I don't understand this condition elif user_choice_index > 2 , invalid values are letters, not numbers so is this statement correct?


#2

I modify the body of play_RPS module to these codes which are on below, so if a user input any values rather than values inside option list (P,S,R), "Invalid option!" will appear on the console.
Also after printing that message on console program again execute play_RPS function so the game can continue.

 if user_choice not in options:
    print
    print "Invalid option!"
    print
  else:
    decide_winner(user_choice, computer_choice)
  return play_RPS()

Also, I remove these line from decide_winner module and the program work perfectly, but I don't why.

elif user_choice_index > 2:
    print "Invalid option!"

So I would like to know your opinion and advice guys about the issue,
please share your thought, thanks.


#3

@mixaxim: Is this from a Python course offered by Rice University? I remember something similar, either way the thing with the numbers is that it will bring the index of the element you were looking for on that list.

Since each position is fixed, they know that on the first position, [0], it's "rock", the second position, [1], it's "paper" and so on.
The thing is: whenever we use index(), we'll try to find a given object, passed as an argument. In case it doesn't exist, it will throw an exception and since you're not handling it, your program will stop, that's why.

Q: But how about my last elif ?
A: You changed your code, now you're checking if the user's input is in your list of options. In case it isn't, it will print out the message "Invalid option!" so having that elif wouldn't make a difference since you're already checking it on your first if.

A try except should help you out in cases where exceptions are thrown, such as that last elif.

But hey, great job! Keep coding! :sun_with_face:


#4

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