DEV Community

Victor Modebe
Victor Modebe

Posted on • Edited on

ROCK PAPER SCISSORS PYTHON GAME

Image description

Greetings guys!!!
This tutorial is for people who are new to python and are looking to learn more about it. I would recommend taking the "learn by doing" method, as practical experimentation solidifies your understanding of a topic.

I will use Visual Studio Code as my IDE, you can use any IDE of your choice. Download a copy of VSCode for your operating system from https://code.visualstudio.com/download

To install Python on your computer, you will need to go to the Python website (https://www.python.org/), download the appropriate installer for your operating system, and run the installer.

Here are the specific steps for Mac, windows, Linux operating systems:

macOS:
1.Go to the Python website: https://www.python.org/downloads/macos/
2.Select the appropriate installer for your operating system architecture.
3.When the download is complete, open the downloaded file (called "python-3.11.1.pkg") and follow the prompts to install Python on your computer.

Windows:
1.Go to the Python website: https://www.python.org/downloads/windows/
2.Select the appropriate installer for your operating system architecture.
3.When the download is complete, open the downloaded file (called "python-3.11.1.exe") and follow the prompts to install Python on your computer.

Linux:
1.Open a terminal window.
2.Use the following command to install Python 3 and pip (the package manager for Python). Make sure to use the latest version of python to install pip. When writing this blog the latest version of python is 3:

sudo apt-get install python3 python3-pip
Enter fullscreen mode Exit fullscreen mode

This will install the latest version of Python 3 and pip. To verify that the installation was successful, you can run the following commands to check the version of Python and pip that are installed:

python3 --version
pip3 --version
Enter fullscreen mode Exit fullscreen mode

These commands should print the version numbers of Python and pip, respectively. If you see version numbers printed, then Python and pip are installed and ready to use.

Now we are done with the installation of Vscode & Python successfully, the next step is to open Vscode create a folder the create a file with .py extension .py as show below.

Image description

Now we dive in to write our code.
Below is a 4 step explanation of the code required for 'ROCK PAPPER SCISSORS' game written in python:

STEP 1
To begin with, we will start by importing the random module. We will also define a custom function 'def get_computer_choice' that takes in the player's choice (either "rock", "paper", or "scissors")and returns the computer's choice, randomly selected from the same three options:

import random

def get_computer_choice(player_choice):
    choices = ["rock", "paper", "scissors"]

    return random.choice(choices)
Enter fullscreen mode Exit fullscreen mode

STEP 2
In this step we define a custom function 'def get_game_result' that takes in the player's choice and the computer's choice, and returns a string representing the outcome of the game. The possible return values are "player wins", "computer wins", and "tie".

def get_game_result(player_choice, computer_choice):
    if player_choice == "rock" and computer_choice == "scissors":
        return "player wins"
    elif player_choice == "rock" and computer_choice == "paper":
        return "computer wins"
    elif player_choice == "paper" and computer_choice == "rock":
        return "player wins"
    elif player_choice == "paper" and computer_choice == "scissors":
        return "computer wins"
    elif player_choice == "scissors" and computer_choice == "rock":
        return "computer wins"
    elif player_choice == "scissors" and computer_choice == "paper":
        return "player wins"
    else:
        return "tie"
Enter fullscreen mode Exit fullscreen mode

STEP 3
In this step we use a while loop which is the main game loop. It continues to run until the player decides to quit by typing 'quit', executed with 'break' an inbuilt- keyword in python .

while True:
    player_choice = input(
        "Please enter your choice (rock, paper, scissors, or quit): ")
    if player_choice.lower() == "quit":
        break
Enter fullscreen mode Exit fullscreen mode

STEP 4
Finally, we define the outcome of the computers choice and print out the game results.

computer_choice = get_computer_choice(player_choice)
    result = get_game_result(player_choice, computer_choice)
    print(f"You chose {player_choice}, the computer chose {computer_choice}.")
    print(f"Result: {result}.")
Enter fullscreen mode Exit fullscreen mode

Congratulations!!! You now have written a python code for 'ROCK PAPER SCISSOR' game. You can play against the computer as many times as you want, and see who comes out on top. Quit the game by typing quit.

If you followed my instructions correctly. This should be how your code will look in VSCode. Also remember, to run your code click the run button on the far right side of the app.

Image description

The results should appear like below

Image description

You can learn more about python from Mosh Hamedani's on python tutorial from his YouTube channel
https://www.youtube.com/watch?v=_uQrJ0TkZlc

Good luck in your Python journey!!!

Top comments (0)