Python Forum

Full Version: Rock, Paper, scissors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is wrong with this Rock, Paper, Scissors game?
Ignore the indentation, it got messed up after copying.

Rock Paper Scissor
def  game():
import  random
Dic =  (1:"R", 2:"P", 3:"S")
PC_Choice = Dic[random.randint(1,  3)]
print  (PC_Choice)  #Debugging
print("Please  type R for Rock, P for Paper or S for Scissors")
User_Choice =  input().upper
if  PC Choice ==  "R"  and  User_Choice
print("The  PC chose rock, its a tie")
elif  PC_Choice ==  "R"  and  User_Choice ==  "P":
print("The PC  chose rock, you win.")
elif  PC_Choice ==  "R"  and  User_Choice ==  "S":
print("The  PC chose rock,  you  lose.")
elif  PC_Choice ==  "P"  and  User_Choice ==  "R":
print("The  PC chose paper, you lose.")
elif  PC_Choice ==  "P"  and  User_Choice ==  "P":
print("The  PC chose paper, its a tie")
elif  PC_Choice ==  "P"  and  User_Choice ==  "S":
print("The  PC chose paper, you win.")
elif  PC_Choice ==  "S"  and  User_Choice ==  "R":
print("The  PC chose scissors, you win.")
elif  PC_Choice ==  "S"  and  User_Choice ==  "P":
print("The  PC chose scissors, you lose.")
elif  PC_Choice ==  "S"  and  User_Choice ==  "S":
print("The  PC chose scissors, it's a tie.")
else:
print("Wait  what?")  #This is for testing the game
game()
game()
#Game loops
The output is always 'Wait, what?', although all of the data was entered correctly.
(Jul-07-2017, 06:37 PM)OmarBrikaa Wrote: [ -> ]Ignore the indentation

One of the things we do not ignore is 'spacing'. It is one of the most important things things in Python. Refer to the Help Document section on how to properly post code.

As it stands, your script will never get past line 1. If line 4 is meant to be a dictionary, use curly braces '{}' not parenthesis '()'. In line 9, you forgot the underscore in "PC_Choice". If you want the script to keep repeating until the 'user' is a winner, use a 'while' loop with 'break' and continue'.

First, post the clean code and we will go from there.
(Jul-07-2017, 06:37 PM)OmarBrikaa Wrote: [ -> ]game()
game()
Do you call game() twice in a row? Or are you calling game() from within itself?

And why didn't you share the error message?
I apologize for the delay. Here is the clean code from my pc.
#Rock Paper Scissor
def game():
   import random
   Dic = {1:"R", 2:"P", 3:"S"}
   PC_Choice = Dic[random.randint(1, 3)]
   print (PC_Choice) #Debugging
   print("Please type R for Rock, P for Paper or S for Scissors")
   User_Choice = input().upper

   if PC_Choice == "R" and User_Choice == "R":
       print("The PC chose rock, it's a tie")

   elif PC_Choice == "R" and User_Choice == "P":
       print("The PC chose rock, you win.")

   elif PC_Choice == "R" and User_Choice == "S":
       print("The PC chose rock, you lose.")

   elif PC_Choice == "P" and User_Choice == "R":
       print("The PC chose paper, you lose.")

   elif PC_Choice == "P" and User_Choice == "P":
       print("The PC chose paper, it's a tie")

   elif PC_Choice == "P" and User_Choice == "S":
       print("The PC chose paper, you win.")

   elif PC_Choice == "S" and User_Choice == "R":
       print("The PC chose scissors, you win.")
   elif PC_Choice == "S" and User_Choice == "P":
       print("The PC chose scissors, you lose.")
   elif PC_Choice == "S" and User_Choice == "S":
       print("The PC chose scissors, it's a tie.")
   else:
       print("Wait what?") #This is for testing the game

   game()
game()
#Game loops
Output:
Wait, what?
You need parentheses after upper on line 8. Right now User_Choice is a method of a string, not the string itself, and is testing false against everything.

If you want to play over and over again, you want a loop, not calling the function from within itself. What you are doing is called recursion, and it is limited in Python, where as loops are not.

while True:
    game()
There are easier ways to do RPS. For example, have a dictionary of what beats what. The you can test if the two choices are equal (a tie), if what the PC's choice beats is equal to what the user chose (PC win), and if neither of those are true then the user wins.
(Jul-10-2017, 12:03 PM)ichabod801 Wrote: [ -> ]You need parentheses after upper on line 8. Right now User_Choice is a method of a string, not the string itself, and is testing false against everything.

Thank you, that fixed it.