Need help with making a rock, paper, scissors bot

Hello, I am working on a beginner level rock, paper, scissors bot.

I have set up everything to what I believe should work but I get a working error that playerhand is undefined. I have gone over it a couple times to look for spelling and syntax errors but I can’t seem to figure out why the code is not recognizing the variable playerhand. If there is something I missed or if I simply did this wrong please let me know so I can learn and not make the same mistake in the future.

Thank you!

For reference this is the code. I have never posted in the forum before so I may be doing this incorrectly, I’ll be making sure to browse the forum more from now on!

import random

#scoring system
Player_score = 0
Cpu_score = 0
Tie_score = 0
#potential options
choices = [“rock”, ‘paper’, “scissors”]

logic needed for decided winner

def checkforwinner(playerhand, Computerhand):
if playerhand == “rock” and Computerhand == “paper”:
print(“You lost try again.”)
return “cpu”
elif playerhand == “rock” and Computerhand == ‘scissors’:
print(“EZPZ LMNSQZ”)
return “player”
elif playerhand == “rock” and Computerhand == “rock”:
print(“You tie’d against a computer???”)
return “tie”

elif playerhand == "scissors" and Computerhand == "rock":
    print("you lose try again")
    return "cpu"
elif playerhand == "scissors" and Computerhand == "paper":
    print("EZ WIN")
    return "player"
elif playerhand == "scissors" and Computerhand == "scissors":
    print("Tie?!?!?")
    return "tie"

elif playerhand == "paper" and Computerhand == "rock":
    print("YUH THATS WHAT I'm TALKIN ABOUT")
    return "player"
elif playerhand == "paper" and Computerhand == "scissors":
    print("tough loss!!")
    return "cpu"
elif playerhand == "paper" and Computerhand == "paper":
    print("Forget about this tie and try again")
    return "tie"
else:
    print(Type your move and try again")

#This is what runs the game#

while Player_score != 3 and Cpu_score != 3:
  while True:
     playerhand = input("\n Pick Rock, Paper, or Scissors. \n first to 3 wins!")
     if (playerhand == "rock" or playerhand == "scissors" or playerhand == "paper"):
       break
     else:
       print("Typed the word, try again")

#time to create computer hand

Computerhand = random.choice(choices)

#displaying the hands

print("Your hand is: ", playerhand)
print("CPU hand is: ", Computerhand)
result = checkforwinner(playerhand, Computerhand)
if (result == “player”):
Player_score += 1
elif (result == “cpu”):
Cpu_score += 1
else:
Tie_score += 1

print("Your score is: ", Player_score, "CPU score is : ", Cpu_score, “Ties :”, Tie_score)

print(“Thanks for playing loser”)

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Indentation matters a lot in Python, so it is difficult to identify whether you have an indentation issue. Properly formatted code will preserve the indentation.

Have a look at this post on how to format code in forum posts:

Short version: Click the </> button in the post editor and copy paste your entire code so that you will end up with something like:

```
Paste your code in between the triple backticks
The opening and closing backticks should be on
blank lines.
```

e.g. See how the indentation is retained when pasting between two sets of triple backticks.

def myFunc(x):
    x += 6
    return x
print(myFunc(3))