Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python rock paper scissors with a rules dictionary

The computer picks randomly and a dictionary stores what beats what, so the winner is found without a long list of if statements. It shows how data can replace repeated code.

Make it yourself: 8 stepsGet a ready-made kit: Robotics and Coding

A drawing for computing and coding projects, not a photograph of this model.

What it shows

The computer picks randomly and a dictionary stores what beats what, so the winner is found without a long list of if statements. It shows how data can replace repeated code.

The science

The random.choice function picks one item from a list with equal chance, so over many rounds the computer should win, lose and draw about one third of the time each. The dictionary maps each move to the move it beats, so one lookup decides the winner. Keeping counts of wins in variables lets you test whether the game is fair.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • A notebook and a pencil

Safety

Read before you start

This project uses safe, everyday materials. Still, read the rules below with a parent before you start.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create rps.py and write import random, then beats = {"rock": "scissors", "paper": "rock", "scissors": "paper"}.

  2. Set you = 0, bot = 0 and draws = 0, then loop with for round_number in range(10):.

  3. Inside, read your move with move = input("rock, paper or scissors? ").lower() and check it with if move not in beats: print("Invalid"); continue.

  4. Let the computer choose with computer = random.choice(list(beats)) and print("Computer chose", computer).

  5. Decide: if move == computer: print("Draw") and draws += 1.

  6. Add elif beats[move] == computer: print("You win") and you += 1, then else: print("Computer wins") and bot += 1.

  7. After the loop print the three counts and announce the overall winner.

  8. Change the loop to let the computer play itself 3000 times without input (take move = random.choice(list(beats)) too) and print the percentages to test fairness.

What you should see

Expected result

The game plays ten rounds and prints who won each. In the 3000 round simulation each outcome comes out near 33 percent, showing that random choice is fair.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

01What does random.choice do?

It picks one item from a list at random.

02Why use a dictionary for the rules?

One lookup replaces three separate if checks.

03What does continue do?

It skips the rest of the loop and starts the next round.

04Why does the 3000-game test come near one third?

With equal chances, large numbers of trials match the expected probability.

Free ebook: Viva Questions for Science Projects

Take it further

Make it your own

Add lizard and Spock with their rules and check by simulation that the game stays fair.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

Find out more about computing and coding

Free, well-known places to explore the subject further. Ask a grown-up before you sign up for anything or type your name anywhere.

WhatsApp us