DEV Community

Ghazal Abdulraheem
Ghazal Abdulraheem

Posted on

Build Rock Paper Scissors

Today’s lesson was very fun and interactive because we explored how Python can make decisions randomly and how we can store multiple values using lists.

The final project for today was building the popular Rock Paper Scissors game, where the user plays against the computer. This project was exciting because it introduced randomness into programming, making the game feel different every time it runs.

The topics covered in today’s lesson include:

  1. Random Module
  2. Understanding the Offset and Appending Items to Lists
  3. Who Will Pay the Bill?
  4. IndexErrors and Nested Lists
  5. Final Project — Rock Paper Scissors

Let’s dive into each topic.


Random Module

Python has a built-in module called random which allows us to generate random values in our programs.

To use the module, we first import it:

import random
Enter fullscreen mode Exit fullscreen mode

One of the most commonly used methods is random.randint().

Example:

import random

random_number = random.randint(1, 10)

print(random_number)
Enter fullscreen mode Exit fullscreen mode

This program generates a random number between 1 and 10.

The random module is very useful for:

  • Games
  • Password generators
  • Simulations
  • Random selections

Another useful method is random.choice() which randomly selects an item from a list.

Example:

import random

names = ["Temmy", "John", "Sarah"]

print(random.choice(names))
Enter fullscreen mode Exit fullscreen mode

Output may vary each time the program runs.


Understanding the Offset and Appending Items to Lists

A list in Python is used to store multiple items inside a single variable.

Example:

fruits = ["Apple", "Orange", "Banana"]
Enter fullscreen mode Exit fullscreen mode

Lists use something called offset or index positions.

Python starts counting from 0, not 1.

Example:

fruits = ["Apple", "Orange", "Banana"]

print(fruits[0])
Enter fullscreen mode Exit fullscreen mode

Output:

Apple
Enter fullscreen mode Exit fullscreen mode

Why?

Because:

  • Apple is at index 0
  • Orange is at index 1
  • Banana is at index 2

You can also modify items in a list.

Example:

fruits[1] = "Mango"

print(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

['Apple', 'Mango', 'Banana']
Enter fullscreen mode Exit fullscreen mode

Appending Items to Lists

Python allows us to add new items to a list using .append().

Example:

fruits.append("Grape")

print(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

['Apple', 'Mango', 'Banana', 'Grape']
Enter fullscreen mode Exit fullscreen mode

Lists are very important in programming because they help us manage collections of data easily.


Who Will Pay the Bill?

One of the mini-projects from today’s lesson was creating a program that randomly selects who will pay the bill.

Instead of arguing among friends, Python makes the decision randomly.

Example:

import random

friends = ["Temmy", "John", "Sarah", "David"]

person = random.choice(friends)

print(f"{person} will pay the bill today.")
Enter fullscreen mode Exit fullscreen mode

Possible Output:

Sarah will pay the bill today.
Enter fullscreen mode Exit fullscreen mode

This simple exercise helped me better understand:

  • Lists
  • Random selections
  • Variables

IndexErrors and Nested Lists

An IndexError happens when we try to access an index position that does not exist inside a list.

Example:

fruits = ["Apple", "Orange", "Banana"]

print(fruits[5])
Enter fullscreen mode Exit fullscreen mode

Output:

IndexError: list index out of range
Enter fullscreen mode Exit fullscreen mode

This error occurs because index 5 does not exist in the list.


Nested Lists

A nested list means placing lists inside another list.

Example:

fruits = ["Apple", "Orange"]
vegetables = ["Tomato", "Carrot"]

food = [fruits, vegetables]

print(food)
Enter fullscreen mode Exit fullscreen mode

Output:

[['Apple', 'Orange'], ['Tomato', 'Carrot']]
Enter fullscreen mode Exit fullscreen mode

Nested lists are useful when organizing grouped data.


Final Project — Rock Paper Scissors

The final project for today was building the classic Rock Paper Scissors game.

The game works like this:

  • The user chooses Rock, Paper, or Scissors
  • The computer randomly makes its own choice
  • Python compares both choices
  • The winner is displayed

This project combined everything learned today:

  • Random module
  • Lists
  • Conditional statements
  • User input
  • Logical comparisons

Here is a simplified version of the project:

import random

choices = ["Rock", "Paper", "Scissors"]

user_choice = input("Choose Rock, Paper, or Scissors: ")

computer_choice = random.choice(choices)

print(f"Computer chose {computer_choice}")

if user_choice == computer_choice:
    print("It's a draw!")

elif user_choice == "Rock" and computer_choice == "Scissors":
    print("You win!")

elif user_choice == "Paper" and computer_choice == "Rock":
    print("You win!")

elif user_choice == "Scissors" and computer_choice == "Paper":
    print("You win!")

else:
    print("You lose!")
Enter fullscreen mode Exit fullscreen mode

One thing I enjoyed about today’s lesson was seeing how randomness can make programs feel more alive and interactive.

The Rock Paper Scissors project also helped me understand how powerful lists and conditions are when building games and applications.

Top comments (0)