Skip to content

Instantly share code, notes, and snippets.

@utkarshmalik211
Created December 22, 2015 07:47
Show Gist options
  • Save utkarshmalik211/587cfb524744179402eb to your computer and use it in GitHub Desktop.
Save utkarshmalik211/587cfb524744179402eb to your computer and use it in GitHub Desktop.
Rock paper scissors game in python
"""Author : Utkarsh Malik
Monday , 21st Dec 2015 9:46PM
"""
from random import randint
print("Welcome to the rock paper scissor game\a\n")
repeat="Y"
y=1
while y==1:
print("For Player vs Cpu enter 1.\nFor Player1 vs Player2 enter 2\n")
try:
gameType = int(input(""))
except ValueError :
print("Invalid choice.\n")
try:
y=int(input("Type 1 to try again : "))
except:
print("Too much userend errors.\nEXIT!!")
y=2
else:
continue
except KeyboardInterrupt :
print("Good bye.Have a nice day\n")
break
else:
if gameType == 1:
while repeat=="Y":
try:
playerchoice=input("Type :\nR for Rock\nP for Paper\nS for Scissors\n: ")
playerchoice=playerchoice.upper()
except KeyboardInterrupt:
print("Bbyeee...\n")
y=2
break
else :
if playerchoice != "R" and playerchoice != "S" and playerchoice != "P":
print("Please enter only R/P/S")
continue
cpuchoiceint=randint(0,8)
if cpuchoiceint < 2:
cpuchoice="R"
elif cpuchoiceint >= 2 and cpuchoiceint < 5:
cpuchoice="P"
elif cpuchoiceint >= 5 :
cpuchoice="S"
if cpuchoice == playerchoice :
print ("There was a tie")
elif cpuchoice=="R" and playerchoice=="P":
print("You win\n")
elif cpuchoice=="R" and playerchoice=="S":
print("Cpu wins\n")
elif cpuchoice=="P" and playerchoice=="R":
print("Cpu wins\n")
elif cpuchoice=="P" and playerchoice=="S":
print("You win\n")
elif cpuchoice=="S" and playerchoice=="R":
print("You win\n")
elif cpuchoice=="S" and playerchoice=="P":
print("Cpu wins\n")
repeat=input("Play again ?\nType Y/N : ")
repeat=repeat.upper()
if repeat=="N":
y=2
break
elif gameType==2:
while repeat=="Y":
print("Type :\nR for Rock\nP for Paper\nS for Scissors\n")
try:
playerchoice1=input("Player 1 : ")
playerchoice1=playerchoice1.upper()
except KeyboardInterrupt:
print("Bbyeee...\n")
y=2
break
else :
try:
playerchoice2=input("Player 2 : ")
playerchoice2=playerchoice2.upper()
except KeyboardInterrupt:
print("Bbyeee...\n")
y=2
break
else:
if (playerchoice1 != "R" and playerchoice1 != "S" and playerchoice1 != "P") or (playerchoice2 != "R" and playerchoice2 != "S" and playerchoice2 != "P"):
print("invalid entry\n")
continue
if playerchoice1 == playerchoice2 :
print ("There was a tie")
elif playerchoice1=="R" and playerchoice2=="P":
print("Player 2 Wins\n")
elif playerchoice1=="R" and playerchoice2=="S":
print("Player 1 Wins\n")
elif playerchoice1=="P" and playerchoice2=="R":
print("Player 1 Wins\n")
elif playerchoice1=="P" and playerchoice2=="S":
print("Player 2 Wins\n")
elif playerchoice1=="S" and playerchoice2=="R":
print("Player 2 Wins\n")
elif playerchoice1=="S" and playerchoice2=="P":
print("Player 1 Wins\n")
repeat=input("Play again ?\nType Y/N : ")
repeat=repeat.upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment