Better Programming

Advice for programmers.

Follow publication

From 30 to 11 Lines of Code: Rock Paper Scissors in Python

Revisiting the popular beginner game

David Amos
Better Programming
Published in
9 min readApr 18, 2022

A brown rock, crumpled white piece of paper, and scissors with blue handles on a red background.
Image by Mariet Jieopp for Getty Images (used with Canva Pro license)

When you learn to program for the first time, you look for — or, perhaps, are assigned — projects that reinforce basic concepts. But how often do you, once you’ve attained more knowledge and experience, revisit those beginner projects from the perspective of an advanced programmer?

In this article, I want to do just that. I want to revisit a common beginner project — implementing the game “Rock Paper Scissors” in Python — with the knowledge I’ve gained from nearly eight years of Python programming experience.

The Rules Of “Rock Paper Scissors”

Before diving into code, let’s set the stage by outlining how “Rock Paper Scissors” is played. Two players each choose one of three items: rock, paper, or scissors. The players reveal their selection to each other simultaneously and the winner is determined by the following rules:

  1. Rock beats scissors
  2. Scissors beats paper
  3. Paper beats rock

Growing up, my friends and I used “Rock Paper Scissors” to solve all sorts of problems. Who gets to play first in a one-player video game? Who gets the last can of soda? Who has to go pick up the mess we just made? Important stuff.

The Requirements

Let’s lay out some requirements for the implementation. Rather than building a full-blown game, let’s focus on writing a function called play() that accepts two string arguments — the choice of "rock", "paper", or "scissors" selected by each player — and returns a string indicating the winner (e.g., "paper wins") or if the game results in a tie (e.g., "tie").

Here are some examples of how play() is called and what it returns:

If one or both of the two arguments are invalid, meaning they aren’t one of "rock", "paper", or "scissors", then play() should raise some kind of…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

David Amos
David Amos

Written by David Amos

Professional technical writer, amateur everything else. Read my mind at https://thoughtcicles.xyz.

No responses yet

Write a response