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.
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.
- Ask
Rock, paper or scissors?. Capitals, and spaces at the ends, don't matter.quitstops the game. - Anything else gets
Rock, paper or scissors. Nothing else.and doesn't count as a round. - 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.orA draw.Rock beats scissors, scissors beat paper, paper beats rock. - Write
winner(player, machine): it returns"you","me"or"draw". Keep the score in a dictionary with those three as its keys. - The game ends as soon as either side has three wins. Draws don't count towards three.
- At the end, print the score,
You 3, me 1, draws 2., thenYou're off the coffee run.if you got to three, orYou're on the coffee run.if the machine did. Quitting early prints only the score.
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
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 whateverwinnerholds, here"Caitlin", sowins[winner] = wins[winner] + 1adds 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 calledwinner. Without quotes,winneris a variable, and Python uses what it holds. Change line 2 towinner = "Yahya"and run it.Keeping score
The machine's score, with a key for each side and one for draws.
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 samerandommodule asrandint, 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.
DRINKSis 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.choicepicks once each time it's called. A variable that holds the result keeps that one, however often you read it.A hand nobody chose
The machine's hand, picked at random from
HANDS.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, soTailscounts astails.continuejumps back to the top of the loop, so a call likeedgenever reaches the toss.resulthands back"you"or"me", which are keys ofscore, 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.
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
paperthree 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.
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, andVERDICTSturns a result into what the machine says. Keep playing while neither side has three wins: awhiletest withand.winneris 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 withrandom.choice, once per round.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?