Rock, paper, scissors
Put together everything you have learnt, and make a game of rock, paper, scissors against the computer.
Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 6: Rock, paper, scissors.
This lesson takes about an hour. The extensions take about 30 minutes more.
What you will learn
- How to plan a program before you write it.
- How to check two things at once with
and. - How to accept an answer in any capital letters with
.lower(). - How to put all your skills together in one game.
What you need from before
input()and f-strings, from Lesson 2.if,elifandelse, from Lesson 3.random.choice(), from Lesson 4.whileloops and counting, from Lesson 5.
Step 1: Plan the game
Good programmers plan first. Type the plan as comments, then fill in the code under each one as you go:
# Rock, paper, scissors
# 1. Set both scores to 0.
# 2. Repeat for 5 rounds:
# - Ask the player for their move.
# - Let the computer choose a move.
# - Work out who won, and add to their score.
# 3. Say who won the game.
Step 2: One round
Start with a single round. .lower() turns the player's answer into small
letters, so Rock and ROCK both become rock:
import random
player = input("rock, paper or scissors? ").lower()
computer = random.choice(["rock", "paper", "scissors"])
print(f"The computer chose {computer}.")
Run it a few times.
Step 3: Who won?
If both chose the same, it's a draw. Otherwise, the player wins in three
ways. and checks that both things are true:
if player == computer:
print("It's a draw!")
elif player == "rock" and computer == "scissors":
print("Rock blunts scissors. You win this round!")
elif player == "paper" and computer == "rock":
print("Paper wraps rock. You win this round!")
elif player == "scissors" and computer == "paper":
print("Scissors cut paper. You win this round!")
else:
print("The computer wins this round!")
In Scratch
and is like the green and block from Operators.
Step 4: Keep score
Make two scores at the top of your program, after import random:
player_score = 0
computer_score = 0
Then add 1 to the right score in each place someone wins, such as
player_score = player_score + 1 under each "You win this round!".
Step 5: Five rounds
Now repeat the round 5 times. Make a round_number variable that starts
at 1, and put your round inside a while loop:
round_number = 1
while round_number <= 5:
print()
print(f"Round {round_number}")
# Your round goes here, indented.
round_number = round_number + 1
To indent lines you have already written, select them and press Tab.
Step 6: The winner
After the loop, not indented, say the final score and who won:
print(f"Final score: you {player_score}, computer {computer_score}")
if player_score > computer_score:
print("You are the champion!")
elif player_score < computer_score:
print("The computer wins this time.")
else:
print("It's a tie!")
Check your code
# Rock, paper, scissors
import random
player_score = 0
computer_score = 0
round_number = 1
print("Let's play rock, paper, scissors! Best of five rounds.")
while round_number <= 5:
print()
print(f"Round {round_number}")
player = input("rock, paper or scissors? ").lower()
computer = random.choice(["rock", "paper", "scissors"])
print(f"The computer chose {computer}.")
if player == computer:
print("It's a draw!")
elif player == "rock" and computer == "scissors":
print("Rock blunts scissors. You win this round!")
player_score = player_score + 1
elif player == "paper" and computer == "rock":
print("Paper wraps rock. You win this round!")
player_score = player_score + 1
elif player == "scissors" and computer == "paper":
print("Scissors cut paper. You win this round!")
player_score = player_score + 1
else:
print("The computer wins this round!")
computer_score = computer_score + 1
round_number = round_number + 1
print()
print(f"Final score: you {player_score}, computer {computer_score}")
if player_score > computer_score:
print("You are the champion!")
elif player_score < computer_score:
print("The computer wins this time.")
else:
print("It's a tie!")
Extensions
A dramatic countdown
time.sleep(0.5) waits half a second. import time at the top, then print
"Rock...", "Paper...", "Scissors...", "SHOOT!" with a pause between each,
before the computer shows its move.
Then change the game so it keeps going until someone has 3 wins. or
means "either one is true", so you can check all three ways to win in
one elif.
One way to do it
# Rock, paper, scissors, with a countdown
import random
import time
player_score = 0
computer_score = 0
print("Rock, paper, scissors! First to 3 wins.")
while player_score < 3 and computer_score < 3:
print()
player = input("rock, paper or scissors? ").lower()
print("Rock...")
time.sleep(0.5)
print("Paper...")
time.sleep(0.5)
print("Scissors...")
time.sleep(0.5)
print("SHOOT!")
computer = random.choice(["rock", "paper", "scissors"])
print(f"The computer chose {computer}.")
if player == computer:
print("It's a draw!")
elif (
(player == "rock" and computer == "scissors")
or (player == "paper" and computer == "rock")
or (player == "scissors" and computer == "paper")
):
print("You win this round!")
player_score = player_score + 1
else:
print("The computer wins this round!")
computer_score = computer_score + 1
print(f"You {player_score}, computer {computer_score}")
if player_score == 3:
print("You are the champion!")
else:
print("The computer wins this time.")
No silly moves
At the moment, typing banana means the computer wins the round. Can you
make the game say "That's not a move!" instead, and not count the round?
Extra bits
You've finished Python Basics!
You can now show messages, ask questions, make decisions, use random numbers and repeat things with loops. That's enough to make a lot of games. Next, Python Toolkit gives you lists, functions, dictionaries and files.
Export your project
pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.
Next time, choose ⇪ Import a project on the home page to carry on with it.