Confident ยท ages 11+ ยท dictionaries ยท functions ยท lists of pairs
Rock, paper, scissors in Python.
Work out who wins each round of rock, paper, scissors from a list of moves.
The program
Press Run to see what it prints. Change anything you like, then run it again.
beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
def winner(a, b):
if a == b:
return "draw"
if beats[a] == b:
return "player 1"
return "player 2"
rounds = [("rock", "paper"), ("paper", "paper"), ("scissors", "paper"), ("rock", "scissors")]
score = {"player 1": 0, "player 2": 0, "draw": 0}
for a, b in rounds:
result = winner(a, b)
score[result] = score[result] + 1
print(a, "vs", b, "->", result)
print(score)rock vs paper -> player 2
paper vs paper -> draw
scissors vs paper -> player 1
rock vs scissors -> player 1
{'player 1': 2, 'player 2': 1, 'draw': 1}
How it works
- The
beatsdictionary says what each move beats. Three rules replace nine cases. - The function returns who won. The loop keeps a score in another dictionary.
- Each pair in
roundsis a tuple, andfor a, b in roundssplits it into two variables.
Try this
- Add lizard and Spock.
- Add more rounds.
- Print who won overall.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
Temperature converter
Turn Celsius into Fahrenheit and print a conversion table.
Beginner ยท ages 9+Palindrome checker
Check whether a word reads the same forwards and backwards.
Easy ยท ages 10+A calculator with functions
Write functions for add, subtract, multiply and divide, and use a dictionary to choose one.
Easy ยท ages 10+FAQ
Rock, paper, scissors: questions
What does the rock, paper, scissors program do?
Work out who wins each round of rock, paper, scissors from a list of moves. It prints: rock vs paper -> player 2 / paper vs paper -> draw / scissors vs paper -> player 1 โฆ
Which Python ideas does it use?
dictionaries, functions, lists of pairs. Confident level, suitable from about age 11.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Add lizard and Spock. Add more rounds. Print who won overall.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay