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:
- Random Module
- Understanding the Offset and Appending Items to Lists
- Who Will Pay the Bill?
- IndexErrors and Nested Lists
- 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
One of the most commonly used methods is random.randint().
Example:
import random
random_number = random.randint(1, 10)
print(random_number)
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))
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"]
Lists use something called offset or index positions.
Python starts counting from 0, not 1.
Example:
fruits = ["Apple", "Orange", "Banana"]
print(fruits[0])
Output:
Apple
Why?
Because:
-
Appleis at index0 -
Orangeis at index1 -
Bananais at index2
You can also modify items in a list.
Example:
fruits[1] = "Mango"
print(fruits)
Output:
['Apple', 'Mango', 'Banana']
Appending Items to Lists
Python allows us to add new items to a list using .append().
Example:
fruits.append("Grape")
print(fruits)
Output:
['Apple', 'Mango', 'Banana', 'Grape']
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.")
Possible Output:
Sarah will pay the bill today.
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])
Output:
IndexError: list index out of range
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)
Output:
[['Apple', 'Orange'], ['Tomato', 'Carrot']]
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!")
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)