Creating a Rock Paper Scissors GAme (Python Lesson)
NOTE: A lesson plan to support this activity can be found by here: Rock Paper Scissors Lesson
Introduction:
How the game works
Rock-paper-scissors is a simple and popular hand game typically played between two people. The objective of the game is to outsmart your opponent by choosing a hand gesture that beats their choice. Here's how the game works:
How the game works
Rock-paper-scissors is a simple and popular hand game typically played between two people. The objective of the game is to outsmart your opponent by choosing a hand gesture that beats their choice. Here's how the game works:
- Players: The game is played between two players, often referred to as Player 1 and Player 2.
- Hand Gestures: Each player simultaneously makes one of three hand gestures representing rock, paper, or scissors:
- Rock: Form a closed fist with your hand.
- Paper: Extend your hand fully with fingers spread out.
- Scissors: Extend your hand and separate your index and middle finger to mimic a scissor shape.
- Winning Rules:
- Rock beats Scissors: Rock smashes scissors, so if a player chooses rock and the other chooses scissors, the player with rock wins.
- Paper beats Rock: Paper covers rock, so if a player chooses paper and the other chooses rock, the player with paper wins.
- Scissors beats Paper: Scissors cut paper, so if a player chooses scissors and the other chooses paper, the player with scissors wins.
- Ties: If both players choose the same gesture, it results in a tie, and the game is usually replayed.
Outline: Rock-Paper-Scissors Game
In this tutorial, you will learn how to create a rock-paper-scissors game using Python. You will understand the basic concepts of conditionals, loops, and user input.
Starter:
Start by playing a game of Rock Paper Scissors against the computer: Rock Paper Scissors with AI (rockpaperscissors-ai.vercel.app)
In this tutorial, you will learn how to create a rock-paper-scissors game using Python. You will understand the basic concepts of conditionals, loops, and user input.
Starter:
Start by playing a game of Rock Paper Scissors against the computer: Rock Paper Scissors with AI (rockpaperscissors-ai.vercel.app)
Activity 1: Introducing the Basics
- Let's start by taking input from the user in Python using the input() function.
- Copy the following code sample into the code window below:
user_choice = input("Enter your choice (rock, paper, or scissors): ")
print("You chose: ", user_choice) |
4. Run the code and observe the output.
5. Comment your code, using the hashtag (#), to explain what the code is doing.
5. Comment your code, using the hashtag (#), to explain what the code is doing.
Activity 2: Implementing the Game Logic
- Let's add some conditional statements (if, elif, else) to compare the user's choice with the computer's choice to determine the winner.
- Here is an example:
import random
user_choice = input("Enter your choice (rock, paper, or scissors): ") computer_choice = random.choice(["rock", "paper", "scissors"]) print("You chose:", user_choice) print("Computer chose:", computer_choice) if user_choice == computer_choice: print("It's a tie!") elif (user_choice == "rock" and computer_choice == "scissors") or \ (user_choice == "paper" and computer_choice == "rock") or \ (user_choice == "scissors" and computer_choice == "paper"): print("You win!") else: print("Computer wins!") |
- Copy the sample code into the code window above.
- Run the code, play the game, and observe the output.
- Comment your code to explain what it is doing.
Activity 3: Adding a Play Again Option
- Let's add a loop to allow the players to play the game multiple times. A while loop can be used to repeatedly play the game until the player decides to stop.
- Here is some sample code:
import random
while True: user_choice = input("Enter your choice (rock, paper, or scissors): ") computer_choice = random.choice(["rock", "paper", "scissors"]) print("You chose:", user_choice) print("Computer chose:", computer_choice) if user_choice == computer_choice: print("It's a tie!") elif (user_choice == "rock" and computer_choice == "scissors") or \ (user_choice == "paper" and computer_choice == "rock") or \ (user_choice == "scissors" and computer_choice == "paper"): print("You win!") else: print("Computer wins!") play_again = input("Do you want to play again? (yes/no): ") if play_again != 'yes': break |
- Copy the sample code into the code window above.
- Run the code, play the game, and observe the output.
- Comment your code to explain what it is doing.
Example Solutions

rock_paper_scissors.zip |
Extension:
Test what happens if you answer 'Yes' or 'YES' when asked if you want to play the game again.
1. Modify your code to fix the error.
2. Add a variable to keep track of the score.
Hint:
if play_again.lower() != 'yes':
break |
2. Comment your code, using the hashtag (#), explaining what the code is doing.
Additional challenge
Create a game of Rock Paper Scissors Lizard Spock.
Additional challenge
Create a game of Rock Paper Scissors Lizard Spock.
You may also be interested in:
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
The Shakespearean Insult Generator is licenced under a Creative Commons Attribution 4.0 International License.
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Tags: programming language python, python programming language, python coding language, programming languages python, python code language, python programming language example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding, best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer.