Play the classic hand game against the computer. First to 3 points wins the match!
Rock, Paper, Scissors is one of the most famous games in the world. Rock smashes scissors, scissors cut paper, and paper wraps rock. In this project you will teach Python to play it against you. The computer makes its choice at random, so it can't cheat, and the first player to reach 3 points wins the match.
The tricky part of this game is working out who wins each round. You could write a long list of if statements, but there is a neater way: a dictionary that remembers what each choice beats. One short line of code can then decide the winner. You will see how picking the right way to store information can make a program much simpler.
You will also use random.choice() to let the computer choose, a while loop to keep the match going, and the continue command to ignore choices that aren't allowed. By the end you will have a game you can play again and again.
We put the three choices in a list. random.choice() picks one item from the list at random, just like closing your eyes and pointing. Run it a few times and you'll see different answers.
import random
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
print("Computer chose", computer)
Computer chose scissors
đ˛ The computer's choice is random, so yours may be different!
We ask the player to type rock, paper or scissors. .lower() turns it into small letters so that "Rock" still counts. Then we check that the answer is actually in the list of choices.
choices = ["rock", "paper", "scissors"]
player = input("rock, paper or scissors? ").lower()
if player in choices:
print("You chose", player)
else:
print("That's not a choice!")
rock, paper or scissors? Rock You chose rock
This dictionary says what each choice beats: rock beats scissors, paper beats rock, and scissors beat paper. So wins_against[player] tells us which choice the player can beat. If that is what the computer picked, the player wins!
wins_against = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
player = "paper"
computer = "rock"
if player == computer:
print("It's a tie! đ¤")
elif wins_against[player] == computer:
print("You win this round! đ")
else:
print("Computer wins this round! đ¤")
You win this round! đ
We need two score variables, one for the player and one for the computer. Whoever wins the round gets 1 point added to their score. After every round we print the scores so everyone knows who is ahead.
player_score = 0
computer_score = 0
# ...when the player wins a round:
player_score = player_score + 1
# ...when the computer wins a round:
computer_score = computer_score + 1
print("Score - You:", player_score, " Computer:", computer_score)
The while loop keeps playing rounds while both scores are less than 3. If the player types something that isn't allowed, continue skips the rest of the loop and jumps straight back to the top to ask again, so the computer doesn't get a free turn.
while player_score < 3 and computer_score < 3:
player = input("rock, paper or scissors? ").lower()
if player not in choices:
print("That's not a choice! Try again.")
continue
# ...the computer chooses and we decide the winner...
When the loop ends, one player has reached 3 points. We only need to check one score to know who won the match. Then we print a message for the winner.
player_score = 3
computer_score = 1
if player_score == 3:
print("đ You won the match!")
else:
print("đģ The computer won the match. Play again for a rematch!")
đ You won the match!
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
import random
choices = ["rock", "paper", "scissors"]
wins_against = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
player_score = 0
computer_score = 0
print("âââī¸ Rock, Paper, Scissors!")
print("First to 3 points wins the match.")
while player_score < 3 and computer_score < 3:
print()
player = input("rock, paper or scissors? ").lower()
if player not in choices:
print("That's not a choice! Try again.")
continue
computer = random.choice(choices)
print("Computer chose", computer)
if player == computer:
print("It's a tie! đ¤")
elif wins_against[player] == computer:
print("You win this round! đ")
player_score = player_score + 1
else:
print("Computer wins this round! đ¤")
computer_score = computer_score + 1
print("Score - You:", player_score, " Computer:", computer_score)
print()
if player_score == 3:
print("đ You won the match!")
else:
print("đģ The computer won the match. Play again for a rematch!")
This is what it looks like when someone plays. The words after each question are what the player typed.
âââī¸ Rock, Paper, Scissors! First to 3 points wins the match. rock, paper or scissors? rock Computer chose scissors You win this round! đ Score - You: 1 Computer: 0 rock, paper or scissors? Paper Computer chose paper It's a tie! đ¤ Score - You: 1 Computer: 0 rock, paper or scissors? banana That's not a choice! Try again. rock, paper or scissors? scissors Computer chose scissors It's a tie! đ¤ Score - You: 1 Computer: 0 rock, paper or scissors? paper Computer chose paper It's a tie! đ¤ Score - You: 1 Computer: 0 rock, paper or scissors? rock Computer chose scissors You win this round! đ Score - You: 2 Computer: 0 rock, paper or scissors? scissors Computer chose scissors It's a tie! đ¤ Score - You: 2 Computer: 0 rock, paper or scissors? paper Computer chose scissors Computer wins this round! đ¤ Score - You: 2 Computer: 1 rock, paper or scissors? rock Computer chose scissors You win this round! đ Score - You: 3 Computer: 1 đ You won the match!
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.