Paul's Python CoursePPC

Project 11 of 13, for Caitlin

Rock Paper Scissors

Rock paper scissors against the computer, first to three: functions, random choices and a score dictionary in one whole game.

About 55 minutes, in 9 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • CaitlinYahya and I play rock paper scissors for the coffee run, and I've won nine days in a row.
  • YahyaThat's because she reads faces. Against a machine it's pure chance, and the numbers will show it.
  • CaitlinFine, build us a machine then. It's not like a machine's got a face.
  • PaulThen it'll need to choose without thinking, and Python's quite good at that.

A game against the machine, first to three wins.

  1. Ask Rock, paper or scissors? . Capitals, and spaces at the ends, don't matter. quit stops the game.
  2. Anything else gets Rock, paper or scissors. Nothing else. and doesn't count as a round.
  3. Otherwise the machine picks its hand with random.choice, fresh every round, and prints it with the result on the same line: I chose scissors. You win., I win. or A draw. Rock beats scissors, scissors beat paper, paper beats rock.
  4. Write winner(player, machine): it returns "you", "me" or "draw". Keep the score in a dictionary with those three as its keys.
  5. The game ends as soon as either side has three wins. Draws don't count towards three.
  6. At the end, print the score, You 3, me 1, draws 2., then You're off the coffee run. if you got to three, or You're on the coffee run. if the machine did. Quitting early prints only the score.
What it prints
Rock, paper or scissors? rock
I chose scissors. You win.
Rock, paper or scissors? Paper
I chose paper. A draw.
Rock, paper or scissors? lizard
Rock, paper or scissors. Nothing else.
Rock, paper or scissors? scissors
I chose rock. I win.
Rock, paper or scissors? quit
You 1, me 1, draws 1.

The lesson, step by step

  1. A dictionary for the score

    A dictionary makes a good scoreboard: one key for each side, each holding a count that starts at 0.

    The game needs a score for you, one for the machine, and one for draws. One dictionary holds all three.

    The key in the square brackets can be a variable. wins[winner] looks up whatever winner holds, here "Caitlin", so wins[winner] = wins[winner] + 1 adds one to Caitlin's count. The example does it twice, and prints {'Caitlin': 2, 'Yahya': 0}.

    The usual slip is to think wins[winner] makes a key called winner. Without quotes, winner is a variable, and Python uses what it holds. Change line 2 to winner = "Yahya" and run it.

  2. Keeping score

    The machine's score, with a key for each side and one for draws.

  3. Pick one of these

    random.choice(a_list) picks one item from a list, each one as likely as the others. It's in the same random module as randint, from Caitlin's guessing game.

    The machine needs a hand nobody chose: rock, paper or scissors, at random.

    The example picks a drink for the Studio's tea round. Run it a few times and it changes. DRINKS is written in capitals because the list never changes while the program runs. That's a habit programmers keep, so a reader knows not to look for it changing.

    The usual slip is the same as with randint. choice picks once each time it's called. A variable that holds the result keeps that one, however often you read it.

  4. A hand nobody chose

    The machine's hand, picked at random from HANDS.

  5. How Paul would do it

    • PaulNeil and I settle tabs against spaces with a coin. It's the same shape of game, just with fewer hands.

    Paul's coin toss is a smaller game with the same parts: a list of choices, a function to pick, a function to decide, and a score dictionary.

    .strip().lower() tidies the answer in one go, so Tails counts as tails. continue jumps back to the top of the loop, so a call like edge never reaches the toss. result hands back "you" or "me", which are keys of score, so one line counts the round whoever won it.

    The labels beside the code are the steps he took. Caitlin's game takes the same steps.

  6. Fix Yahya's version

    • YahyaI've built the machine already. Calling random every round seemed wasteful, so it only picks once.
    • YahyaBut Caitlin played paper three times and won every round, and that's not chance. Can you look?

    Yahya's game plays three rounds. Run it and play paper three times, and watch what the machine chooses.

    Fix it so the machine picks a fresh hand every round. The checks decide in advance what the machine will pick, and expect it to change.

  7. Make the game

    Write the game. The brief has the whole spec.

    Rock, paper or scissors? rock
    I chose scissors. You win.
    Rock, paper or scissors? lizard
    Rock, paper or scissors. Nothing else.
    Rock, paper or scissors? quit
    You 1, me 0, draws 0.
    

    The rules are in the file already. BEATS["rock"] is "scissors", the hand rock beats, and VERDICTS turns a result into what the machine says. Keep playing while neither side has three wins: a while test with and.

    winner is checked on its own, so it returns its answer and prints nothing. The checks also decide the machine's hands in advance, so they can play whole games and know how each ends. That only works if the machine picks with random.choice, once per round.

  8. Ship it

    • CaitlinI played it twice and won once, lost once, and I'm calling that skill.
    • YahyaOne each is exactly what chance looks like, and two games isn't a dataset. Best of five?