Rock, Paper, Scissors is a classic game that has been played for generations. In this tutorial, we will code the game using Python.
First, we need to import the random
module to generate a random move for the computer. We also need to define a list of possible moves:
from random import randint
moves = ["rock", "paper", "scissors"]
Next, we need to get the player's move using the input() function:
player_move = input("Enter your move (rock/paper/scissors): ")
Now, we need to generate a random move for the computer:
computer_move = moves[randint(0,2)]
In this example, we use the randint() function to generate a random integer between 0 and 2, and then use that integer to select a move from the moves list.
Next, we need to determine the winner of the game:
if player_move == computer_move:
print("It's a tie!")
elif player_move == "rock":
if computer_move == "scissors":
print("You win!")
else:
print("Computer wins!")
elif player_move == "paper":
if computer_move == "rock":
print("You win!")
else:
print("Computer wins!")
elif player_move == "scissors":
if computer_move == "paper":
print("You win!")
else:
print("Computer wins!")
In this example, we use a series of nested if statements to determine the winner of the game based on the player's move and the computer's move.
And that's it! With just a few lines of code, we have coded the Rock, Paper, Scissors game in Python. You can use this code as a starting point to create your own version of the game with additional features, such as keeping score and allowing the player to play multiple rounds.