0

My Rock Paper Scissors code doesn't work and I'm assuming it's because I'm using the return value incorrectly. What should I do?

EDIT, so I've stored the return like this

def results (x, y):
if (x == "R" or x == "rock" or x == "r" or x == "Rock" or x == "ROCK") and (y == "S" or y ==  "s" or y == "Scissors" or y == "SCISSORS" or y == "scissors"):
    winner = 1
    return winner

But how do I get "winner" to print outside of the function?

OLD

player1 = input ("Player 1: Please enter either Rock, Paper or Scissors:")

player2 = input ("Player 2: Please enter either Rock, Paper or Scissors:") 

def results (x, y):
 if (x == "R" or x == "rock" or x == "r" or x == "Rock" or x == "ROCK") and (y == "S" or y ==  "s" or y == "Scissors" or y == "SCISSORS" or y == "scissors"):

    return 1
 else:
    if (x == "rock" or x == "r" or x =="R" or x == "Rock" or x == "ROCK") and (y == "P" or y == "p" or y == "paper" or y == "Paper" or y == "PAPER"):

        return 2
    else:
        if (x == "rock" or x =="R" or x == "r" or x == "Rock" or x == "ROCK") and (y == "rock" or y =="R" or y == "r" or y =="Rock" or y == "ROCK"):

            return 0
        else: 
            print ("Sorry, I didn't understand your input") 


results (player1, player2)

if results == 1:
    print ("Player 1 wins!")
else:
    if results == 2:
        print("Player 2 wins!")
    else:
        if results == 0:
            print("It was a tie!")
2
  • 1
    You might benefit from reviewing a tutorial on Python functions - in particular how to call them. This is the official one, there are others out there: docs.python.org/2/tutorial/controlflow.html#defining-functions Commented Feb 3, 2014 at 1:31
  • Also please don't extend if statements that much. Try something like rock_list = ["rock","R","r"] and if x in rock_list: this looks better but it's only style thing so still your choice to use it or not. Commented Feb 3, 2014 at 2:06

1 Answer 1

5

The return value isn't automatically stored anywhere. You need to store it manually:

result = results(player1, player2)

if result == 1:
    ...

If you look up at the top of your code, you'll see you already did the right thing with the input function:

player1 = input ("Player 1: Please enter either Rock, Paper or Scissors:")

Functions you define yourself should be handled the same way.


In response to the edit: Creating a local variable inside results won't help. The code that calls the function needs to store the return value. (People have designed languages that work the way you're trying to get it to work. The result is a huge headache with unrelated parts of the program stomping over each other's data.)

Sign up to request clarification or add additional context in comments.

3 Comments

How do I get the local return variable to be universal?
@Keith: See expanded answer.
Thanks I got it winner = results (player1, player2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.