rok paper scissors game using python

Rock Paper Scissors Game Using Python

Spread the love

In this tutorial I’ll show you how to make a simple rock paper scissors game using python.

Here we need one module:

  • random

We need to use a random module to generate a random name from the given list. to get more info about the random module, you can check its documentation.

#Rock paper scissors game using python

import random

def game(player):
    rps = ["Rock", "Paper", "Scissors"]
    computer = rps[random.randint(0,2)]
    if player == computer:
        return "Tie! computer choose " + computer
    elif player == "Rock":
        if computer == "Paper":
            return "You lose! computer choose " + computer
        else:
            return "You win! computer choose " + computer
    elif player == "Paper":
        if computer == "Scissors":
            return "You lose! computer choose " + computer
        else:
            return "You win! computer choose " + computer
    elif player == "Scissors":
        if computer == "Rock":
            return "You lose computer choose " + computer
        else:
            return "You win! computer choose " + computer
    else:
        return "Maybe you have made any typo"
  
print(game("Rock"))

We started by defining a function named game. inside that, we created a list rps which contains “Stone”, “Paper” and “Scissors” in a string form. From that list, we will generate random things for computer.

After that we used if-else statement to verify who won.

Rock Paper Scissors Game Using Python - Papa Programmer

Related post: How to Make a Simple Calculator Using Python

Still wondering how to get started with python?

Check out our exclusive book,

Rock Paper Scissors Game Using Python - Papa Programmer

Spread the love
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like