0

I'm trying to create a rock, paper, scissors game using functions but when I run the game it doesn't show who won. The function is called "Game()". That is the code:

import random

playerChoice=0
computerChoice=0

computerChoices=["Rock","Paper","Scissors"]

def Game():
    def Rock():
        if playerChoice=="Rock":
            if computerChoice=="Rock":
                print("Tie")
            if computerChoice=="Paper":
                print("You Lost")
            if computerChoice=="Scissors":
                print("You Won")
        else:
            Paper()
    def Paper():
        if playerChoice=="Paper":
            if computerChoice=="Paper":
            print("Tie")
            if computerChoice=="Scissors":
            print("You Lost")
            if computerChoice=="Rock":
            print("You Won")
        else:
            Scissors()
    def Scissors():
        if computerChoice=="Scissors":
            print("Tie")
        if computerChoice=="Rock":
            print("You Lost")
        if computerChoice=="Paper":
            print("You Won")
    Rock()

def Play():
    playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
    computerChoice=random.choice(computerChoices)
    print(f"Computer Picked {computerChoice}")
    Game()

Play()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Welcome to Stack Overflow. This code has indentation errors. Please make sure to post code exactly as you actually have it, since indentation is crucial in Python. We cannot accept code hosted on external sites; the necessary information must be in the question itself. Consider using code fences to format the code; it's a little easier to do by hand. See the [formatting help](https://stackoverflow.com/help/formatting) for details. – Karl Knechtel Jun 11 '22 at 16:28
  • Anyway, the problem is that `Play` does not assign to the global variables. Overall, the organization of the code does not make sense. My recommendation is to follow a Python tutorial from start to finish, and make sure you understand how functions are intended to work. – Karl Knechtel Jun 11 '22 at 16:31

2 Answers2

1

If you examine the choices right before you call Rock() in Game, you'll see that playerChoice and computerChoice are still set to 0. This is because when you do

    playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
    computerChoice=random.choice(computerChoices)

it doesn't actually change the values of the global variables playerChoice or computerChoice. This is called shadowing.

What you can do is accept the choices as arguments:

def Game(playerChoice, computerChoice):
    # ...

def Play():
    playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
    computerChoice=random.choice(computerChoices)
    print(f"Computer Picked {computerChoice}")
    Game(playerChoice, computerChoice)

Also, you have an indentation error in Paper(), it should be this:

    def Paper():
        if playerChoice=="Paper":
            if computerChoice=="Paper":
                print("Tie")
            if computerChoice=="Scissors":
                print("You Lost")
            if computerChoice=="Rock":
                print("You Won")
        else:
            Scissors()

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
0

Your if statements are a bit messed up. Try using elif more.Try studying this.

        import random
        
        user_action = input("Enter a choice (rock, paper, scissors): ")
        possible_actions = ["rock", "paper", "scissors"]
        computer_action = random.choice(possible_actions)
        print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
        
        if user_action == computer_action:
            print(f"Both players selected {user_action}. It's a tie!")
        elif user_action == "rock":
            if computer_action == "scissors":
                print("Rock smashes scissors! You win!")
            else:
                print("Paper covers rock! You lose.")
        elif user_action == "paper":
            if computer_action == "rock":
                print("Paper covers rock! You win!")
            else:
                print("Scissors cuts paper! You lose.")
        elif user_action == "scissors":
            if computer_action == "paper":
                print("Scissors cuts paper! You win!")
            else:
                print("Rock smashes scissors! You lose.")
MrNate
  • 25
  • 6