Member-only story
From 30 to 11 Lines of Code: Rock Paper Scissors in Python
Revisiting the popular beginner game

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:
- Rock beats scissors
- Scissors beats paper
- 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…