Rock-Scissor-Paper made Easy with Python – An Introduction to Randomness in Python

Make your own Rock-Scissor-Paper game with Python using Randomness

How to use Randomness in Python and create a Rock-Scissor-Paper game.

Step 1: Randomness in Python

To make games interesting you need some unpredictable aspects. This is where randomness is used.

Luckily, Python has a library to make randomness. To simulate rolling a die can be done as follows.

import random
die = random.randint(1, 6)
print(die)

Where randint(1, 6) returns a random integer from 1 to 6, both inclusive.

Step 2: The Rock-Scissor-Paper game

I think most know the game. If not, read the rules on the wiki.

Let’s try a game of it.

print("Enter choice \n 1. Rock \n 2. Paper \n 3. Scissor \n")
choice = int(input("Choice: "))
computer_choice = random.randint(1, 3)
if choice == computer_choice:
    print("Draw")
elif choice == 1:
    if computer_choice == 2:
        print("Computer wins, Paper")
    else:
        print("You win, Scissor")
elif choice == 2:
    if computer_choice == 1:
        print("You win, Rock")
    else:
        print("Computer wins, Scissor")
elif choice == 3:
    if computer_choice == 1:
        print("Computer wins, Rock")
    else:
        print("You win, Paper")

It uses simple input statements to get input from the user. You can learn about input statements here. Then it converst the variable, which you can learn about here.

The new thing is using randomness in your Python code. This is what makes coding fun, it becomes unpredictable.

The rest is simple program flows with if-statements, whcih you can learn about here.

Step 3: What is Next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • 2 FREE eBooks to support your Python learning.

See the full FREE course page here.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment