Rock Paper Scissors Game with Python3

In this tutorial, i would be showing you how to create a text-based rock paper scissor game using Python3.

Everyone reading this post must be familiar with the Rock-Paper-Scissor game.

A simple game in which a person chooses from [rock,paper,scissor] and also the second person chooses at random.

In this code snippet we would also be creating a RPS (Rock-Paper-Scissor) game with a player and the computer.


 import random
 def rps():
         while True:
                 user = input("Enter Your Value: ").lower()

                 #define moves that can be chosen
                 moves = ["r","p","s"]
                 #making computer choose a random move
                 comp = random.choice(moves)
                 #testing for win or loss
                 if (comp == user):
                         print("Computer chose: "+comp)
                         print("User chose: "+user)
                         print("Tie")
                 elif user not in moves:
                         print("Please enter from r , p or s!")
                 elif (comp == "r" and user == "s") or (comp == "p" and user == "r") or (comp == "s" and user == "p"):
                         print("Computer chose: "+comp)
                         print("User chose: "+user)
                         print("Computer Won!")
                 else:
                         print("Computer chose: "+comp)
                         print("User chose: "+user)
                         print("User won !")

 #call to function        
 rps()

0 Comments