Back

Activity 1.15: Rock, Paper, Scissors

divider

Activity 1.15

Rock, Paper, Scissors

Key Concepts

Multi-Line Conditions

Playing Again

Watch Your Variable Names

Python has a built-in round() function — naming a variable round would hide it.

round = 5 # Shadows Python's built-in round() function!
round_num = 5 # Better -- doesn't collide with anything

Breaking a Long Condition Into Multiple Lines

Wrapping a condition in parentheses lets you spread it across several lines — handy once it gets long.

elif (
(player == "rock" and computer == "scissors")
or (player == "paper" and computer == "rock")
or (player == "scissors" and computer == "paper")
):
print("-YOU WIN-")

Playing Again

To ask "play again?" we ask a question and turn the answer into a boolean ourselves.

play_again = input("Play again? (y/n):").lower()
playing = play_again == "y"

Today's Objectives

  • Looping a game until the player chooses to stop
  • Picking a random computer move
  • Checking a win condition with a multi-line elif

Key Terms

Built-in Function
A function Python already provides, like round() — avoid naming variables the same thing.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-15-rock-paper-scissors.


Task 1: Set Up the Game

  • Declare the score, round counter, and player/computer choices.
Rock, Paper, Scissors
import random
wins = 0
losses = 0
round_num = 1
playing = True
player = ""
computer = ""
# Rules:
# Rock smashes Scissors
# Paper covers Rock
# Scissors cuts Paper
print("--- Rock, Paper, Scissors ---")

Task 2: Pick a Random Computer Move

  • Loop while playing, asking the player for a move and picking a random one for the computer.
Rock, Paper, Scissors
import random
wins = 0
losses = 0
round_num = 1
playing = True
player = ""
computer = ""
# Rules:
# Rock smashes Scissors
# Paper covers Rock
# Scissors cuts Paper
print("--- Rock, Paper, Scissors ---")
while playing:
print(f"Round {round_num}")
print(f"W: {wins} - L: {losses}")
player = input("(rock, paper, scissors):").lower()
random_num = random.randint(0, 2)
if random_num == 0:
computer = "rock"
elif random_num == 1:
computer = "paper"
else:
computer = "scissors"
print(f"Computer chooses {computer}!")

Task 3: Check the Winner and Play Again

  • Compare the two moves to decide a winner, then ask if the player wants another round.
Rock, Paper, Scissors
import random
wins = 0
losses = 0
round_num = 1
playing = True
player = ""
computer = ""
# Rules:
# Rock smashes Scissors
# Paper covers Rock
# Scissors cuts Paper
print("--- Rock, Paper, Scissors ---")
while playing:
print(f"Round {round_num}")
print(f"W: {wins} - L: {losses}")
player = input("(rock, paper, scissors):").lower()
random_num = random.randint(0, 2)
if random_num == 0:
computer = "rock"
elif random_num == 1:
computer = "paper"
else:
computer = "scissors"
print(f"Computer chooses {computer}!")
if player == computer:
print("-TIE-")
elif (
(player == "rock" and computer == "scissors")
or (player == "paper" and computer == "rock")
or (player == "scissors" and computer == "paper")
):
print("-YOU WIN-")
wins = wins + 1
else:
print("-YOU LOSE-")
losses = losses + 1
round_num = round_num + 1
play_again = input("Play again? (y/n):").lower()
playing = play_again == "y"

Task 4: Final Score

  • Print the final win/loss record once the player stops playing.
Rock, Paper, Scissors
import random
wins = 0
losses = 0
round_num = 1
playing = True
player = ""
computer = ""
# Rules:
# Rock smashes Scissors
# Paper covers Rock
# Scissors cuts Paper
print("--- Rock, Paper, Scissors ---")
while playing:
print(f"Round {round_num}")
print(f"W: {wins} - L: {losses}")
player = input("(rock, paper, scissors):").lower()
random_num = random.randint(0, 2)
if random_num == 0:
computer = "rock"
elif random_num == 1:
computer = "paper"
else:
computer = "scissors"
print(f"Computer chooses {computer}!")
if player == computer:
print("-TIE-")
elif (
(player == "rock" and computer == "scissors")
or (player == "paper" and computer == "rock")
or (player == "scissors" and computer == "paper")
):
print("-YOU WIN-")
wins = wins + 1
else:
print("-YOU LOSE-")
losses = losses + 1
round_num = round_num + 1
play_again = input("Play again? (y/n):").lower()
playing = play_again == "y"
print("--- Final Score ---")
print(f"Wins: {wins} - Losses: {losses}")
print("Thanks for playing!")
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Rock, Paper, Scissors ---
Round 1
W: 0 - L: 0
(rock, paper, scissors): rock [Enter]
Computer chooses scissors!
-YOU WIN-
Play again? (y/n): y [Enter]
Round 2
W: 1 - L: 0
(rock, paper, scissors): paper [Enter]
Computer chooses paper!
-TIE-
Play again? (y/n): y [Enter]
Round 3
W: 1 - L: 0
(rock, paper, scissors): scissors [Enter]
Computer chooses rock!
-YOU LOSE-
Play again? (y/n): n [Enter]
--- Final Score ---
Wins: 1 - Losses: 1
Thanks for playing!
divider

Reflection

Answer the following questions before submitting your work.

  1. Why does round_num use that name instead of just round?
  2. How does playing = play_again == "y" turn a typed answer into a boolean?
  3. Why does the win condition need three separate and pairs joined by or, instead of one simple comparison?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete