Elif SyntaxError


#1



Hello, I've been trying to figure out what's wrong with my code but unfortunately I haven't been able to fix it. Perhaps you can shed some light on the problem, Thx :slight_smile:

Line 27
SyntaxError: invalid syntax

27 elif user_choice_index == 0 and computer_choice_index == 2:


'''the program is a Rock, Paper, Scissors game'''

#function imports
from random import randint
from time import sleep

#list containing the 3 available options for the game
OPTIONS = ['R', 'P', 'S']

#Win/Loss "constant" variable
WIN_MESSAGE= "You have Won!"
LOSS_MESSAGE =  "You have lost! Better luck next time"

def decide_winner(user_choice, computer_choice):
  print "You have selected: %s" % (user_choice)
  print "Computer selecting..."
  sleep(2)
  print "The computer has selected: %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'
    
    # Rock wins over Scissors, index positions 0 and 2
    elif user_choice_index == 0 and computer_choice_index == 2:
      print WIN_MESSAGE
    
    # Paper wins over Rock, index positions 1 and 0
    elif user_choice_index == 1 and computer_choice_index == 0:
      print WIN_MESSAGE + "Paper beats Rock"
    
    # Scissors win over paper, index positions 2 and 1
    elif user_choice_index == 2 and computer_choice_index == 1:
      print WIN_MESSAGE + "Scissors beat Paper"
    
    elif user_choice_index > 2:
      print "That's rubbish, you have entered an invalid option. Now we have to start again"
      return
    
    else:
      print LOSS_MESSAGE
      
def play_RPS():
  print" Welcome to Rock, Paper, Scissors"
 
user_choice = raw_input("Select R for rock, P for paper or S for scissors: ")
  sleep(2)
  user_choice.upper()
  
  # make the PC choose a random index value from the options list
  # computer_choice = options[randint(0,2)]
  
  # This would be a more efficient way to do it as we don't have to worry about the number of elements in the list, even it gets modified at a later date. 
  computer_choice = options[randint(0,len(options)-1)]
  
  decide_winner(user_choice, computer_choice)

play_RPS()


#2

you have an indention problem. If and elif should have the same indention level, given elif should come after if


#3

That fixed the problem, thank you very much!

But I still get an error when running the program and choosing a character different than r, p or s. It says that the value T, for instance, is not in the list.

I suppose that adding every single user choice to the list so that an index value > 2 is assigned would only serve as a short term solution since the more the program is used the bigger the list would grow (I'm not sure how duplicates are handled within lists but I assume every item is considered separately based on index value rather than what the actual item is)

So I was thinking if something like

elif user_choice_index != OPTIONS[:3]

but of course user input is not being checked directly against the contents of the list but rather against the index values and therefore this would yield an error.

What would be the best way to tackle this problem?


#4

What the exercise suggest for validation for invalid user input is crap

After you get the user choice, check if it is in the list:

#lets put upper on the same line as getting the input
user_choice = raw_input("Select R for rock, P for paper or S for scissors: ").upper()
while user_choice not in OPTIONS:
   user_choice = raw_input("That is invalid, Select R for rock, P for paper or S for scissors: ").upper()

#5

That certainly did the trick, tyvm :):smile:


#6