How to create rock paper scissors two players game in Python
In this Python tutorial, we will learn how to create rock paper scissors two player scissor game in Python. So, at first we will define the moves about the rock, paper and scissors and then take a random module to take input as a second player and for the first player, we will define the conditions.
Create rock paper scissors game in Python
Our goal is to define the condition to make a player win or loose in the game. So we will use the condition statement in Python but before that, we need to define the moves firstly.
Defining moves in rock paper scissors – Python
In the moves, we will define the rock, paper and scissors
moves = ["rock", "paper", "scissors"] keep_playing = "true"
Importing random module to take random input
The random module will help us to take the random input for the second player.
import random #defining cpu move as random cmove = random.choice(moves)
Some tutorials related to random module:
Defining conditions in Rock paper scissors game
So, now we will define the condition for both the players
while keep_playing == "true": cmove = random.choice(moves) pmove = input("What is your move: rock, paper or scissors?") print ("The computer chose",cmove) if cmove == pmove: print ("Tie") elif pmove == "rock" and cmove == "scissors": print ("congratulations to the winner Player wins") elif pmove == "rock" and cmove == "paper": print ("congratulations to the winner Computer wins") elif pmove == "paper" and cmove == "rock": print ("congratulations to the winner Player wins") elif pmove == "paper" and cmove == "scissors": print ("congratulations to the winner Computer wins") elif pmove == "scissors" and cmove == "paper": print ("congratulations to the winner Player wins") elif pmove == "scissors" and cmove == "rock": print ("congratulations to the winner Computer wins")
Output:
What is your move: rock, paper or scissors?rock The computer chose scissors congratulations to the winner Player wins
You can also be interested in learning this:
Leave a Reply