'return' outside function


#1



I can't seem to fix this SyntaxError:

SyntaxError: 'return' outside function
$ python RPS.py
File "RPS.py", line 33
return


from random import randint
from time import sleep

OPTIONS = ['R', 'P', 'S']
LOSER = 'You lost!'
WINNER = 'You WIN!'

def decide_winner(user_choice, computer_choice):
  print "You selected: %s" % user_choice
  print "Computer selecting..."
  sleep(1)
  print "Computer selects: %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!"
elif user_choice_index == 0 and computer_choice_index == 2:
  print WINNER
elif user_choice_index == 1 and computer_choice_index == 0:
  print WINNER
elif user_choice_index == 2 and computer_choice_index == 1:
  print WINNER
elif user_choice_index > 2:
  print "Invalid option was selected."
  return
else:
  print LOSER
  
def play_RPS():
  print "Rock, Paper, or Scissors?"
  user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ")
  sleep(1)
  user_choice = user_choice.upper()
  computer_choice = OPTIONS[randint(len(OPTIONS)-1)]
  decide_winner(user_choice, computer_choice)
  
play_RPS


Any suggestions? What am I not seeing?
Thanks so much in advance!!!


#2

the if/elif statements which check for the winner, should be inside your decide_winner function


#3

It's now giving me another syntaxError:

File "RPS.py", line 24
elif user_choice_index == 0 and computer_ch
oice_index == 2:
^
SyntaxError: invalid syntax

def decide_winner(user_choice, computer_choice):
  print "You selected: %s" % user_choice
  print "Computer selecting..."
  sleep(1)
  print "Computer selects: %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 "TIE!"
	elif user_choice_index == 0 and computer_choice_index == 2:
  	print WINNER
	elif user_choice_index == 1 and computer_choice_index == 0:
  	print WINNER
	elif user_choice_index == 2 and computer_choice_index == 1:
  	print WINNER
	elif user_choice_index > 2:
  	print "Invalid option was selected."
  	return
	else:
  	print LOSER

#4

please think about your indent. it was right (except it needed 4 more spaces to be inside the function), now you messed up your whole indention


#5

Not sure why my code came up like that. My indents are not what shows above. See my true code below:


#6

can you copy paste your full code to the forum so i can run it?

Use markup to make the indent right:


#7

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