Question

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!")
Was it helpful?

Solution

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top