In this article, I’ll walk you through how to make a rock, paper, and scissors game with Python. In the rock, paper and scissors game our goal is to create a command-line game where a user has the option to choose between rock, paper and scissors and if the user wins the score is added, and at the end when the user finishes the game, the score is shown to the user.
Rock, Paper and Scissors Game with Python
To create the Rock, Paper and Scissors game with Python, we need to take the user’s choice and then we need to compare it with the computer choice which is taken using the random module in Python from a list of choices, and if the user wins then the score will increase by 1:
Rock, Paper or Scissors?rock
Tie!
Rock, Paper or Scissors?paper
You win! Paper covers Rock
Rock, Paper or Scissors?scissors
You lose… Rock smashes Scissors
Rock, Paper or Scissors?end
Final Scores:
CPU:1
Plaer:1
Creating these types of games will help a beginner to think logically. You can even use this idea to make your own game. In the end, creating these types of programs will help you create your algorithms, which is a very important skill for coding interviews and competitive programming.
Also, Read – 100+ Machine Learning Projects Solved and Explained.
I hope you liked this article on how to create a rock, paper, and scissor game with Python. Feel free to ask your valuable questions in the comments section below.
computer should take random variables as many times user plays the game
but in your code it will take random variable once and plays with that one only
computer = random.choice(choices)
this line should be in while loop
\
Thank You..,
# Small correction is made
import random
choices = [“Rock”, “Paper”, “Scissors”]
player = False
cpu_score = 0
player_score = 0
while True:
computer = random.choice(choices)
player = input(“Rock, Paper or Scissors?”).capitalize()
## Conditions of Rock,Paper and Scissors
if player == computer:
print(“Tie!”)
elif player == “Rock”:
if computer == “Paper”:
print(“You lose!”, computer, “covers”, player)
cpu_score+=1
else:
print(“You win!”, player, “smashes”, computer)
player_score+=1
elif player == “Paper”:
if computer == “Scissors”:
print(“You lose!”, computer, “cut”, player)
cpu_score+=1
else:
print(“You win!”, player, “covers”, computer)
player_score+=1
elif player == “Scissors”:
if computer == “Rock”:
print(“You lose…”, computer, “smashes”, player)
cpu_score+=1
else:
print(“You win!”, player, “cut”, computer)
player_score+=1
elif player==’End’:
print(“Final Scores:”)
print(f”CPU:{cpu_score}”)
print(f”Plaer:{player_score}”)
break
import random
choices = [“Rock”, “Paper”, “Scissors”]
player = False
cpu_score = 0
player_score = 0
while True:
player = input(“Rock, Paper or Scissors?”).capitalize()
random.shuffle(choices)
computer=choices[0]
print(f’Computer Choice is {computer}’)
## Conditions of Rock,Paper and Scissors
if player == computer:
print(“Tie!”)
elif player == “Rock”:
if computer == “Paper”:
print(“You lose!”, computer, “covers”, player)
cpu_score+=1
else:
print(“You win!”, player, “smashes”, computer)
player_score+=1
elif player == “Paper”:
if computer == “Scissors”:
print(“You lose!”, computer, “cut”, player)
cpu_score+=1
else:
print(“You win!”, player, “covers”, computer)
player_score+=1
elif player == “Scissors”:
if computer == “Rock”:
print(“You lose…”, computer, “smashes”, player)
cpu_score+=1
else:
print(“You win!”, player, “cut”, computer)
player_score+=1
elif player==’End’:
print(“Final Scores:”)
print(f”CPU:{cpu_score}”)
print(f”Player:{player_score}”)
break
Output:
Rock, Paper or Scissors?rock
Computer Choice is Paper
You lose! Paper covers Rock
Rock, Paper or Scissors?paper
Computer Choice is Rock
You win! Paper covers Rock
Rock, Paper or Scissors?paper
Computer Choice is Scissors
You lose! Scissors cut Paper
Rock, Paper or Scissors?rock
Computer Choice is Paper
You lose! Paper covers Rock
Rock, Paper or Scissors?rock
Computer Choice is Rock
Tie!
Rock, Paper or Scissors?rock
Computer Choice is Paper
You lose! Paper covers Rock
Rock, Paper or Scissors?end
Computer Choice is Rock
Final Scores:
CPU:4
Player:1
>