Skip to the content.

Simulation

Homework and Hacks

Popcorn Hacks

# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['Go for a walk', 'Read a book', 'Play basketball', 'Paint', 'Watch a movie', 'Take a nap', 'Go for a run', 'Gym', 'Sit outside', 'Listen to music', 'Be productive and do your homework']
# Step 2: Randomly choose an activity
random_activity = random.choice(activities)
# Step 3: Display the chosen activity
print(f"Today’s random activity: {random_activity}")

Today’s random activity: Go for a run

3.

# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['Hannah', 'Gaheera', 'Shriya']
activities = ['sleep', 'code code code', 'eat', 'shadow box', 'tag']
# Randomly shuffle the list of activities to assign them randomly to the guests
random.shuffle(activities)
# Loop through each guest and assign them a random activity
for i in range(len(hosts)):
    print(f"{hosts[i]} will be monitoring {activities[i]}!")
Hannah will be monitoring eat!
Gaheera will be monitoring sleep!
Shriya will be monitoring code code code!

MC : C

MC: D

  1. Dice roller
import random

def roll_dice():
    return random.randint(1, 100)

dice_roll = roll_dice()
print("Number:", dice_roll)
Number: 33

2.

import random

def play_rock_paper_scissors():
    choices = ['rock', 'paper', 'scissors']
    computer_choice = random.choice(choices)
    user_choice = input("Enter your choice (rock, paper, or scissors): ")

    if user_choice not in choices:
        print("Invalid choice. Please try again.")
        return

    print("Computer chose:", computer_choice)
    print("You chose:", user_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("You lose!")

play_rock_paper_scissors()

Computer chose: rock
You chose: paper
You win!

MC: C

MC: D

Random Algorithm Homework 1

import random

# List of 15 students
students = [
    "Alice", "Ben", "Chloe", "David", "Ella",
    "Frank", "Grace", "Hannah", "Isaac", "Jade",
    "Kyle", "Luna", "Mason", "Nina", "Owen"
]

# Creative team names
teams = {
    "Tralalero Tralala": [],
    "Tung Tung Tung Tung Tung Tung Tung Tung Tung Sahur": [],
    "Bombardiro Crocodilo": []
}

# Randomly assign each student to a team
for student in students:
    team_name = random.choice(list(teams.keys()))
    teams[team_name].append(student)

# Print the team assignments
print("Random Team Assignments:\n")
for team, members in teams.items():
    print(f"{team}:")
    for member in members:
        print(f" - {member}")
    print()

Random Team Assignments:

Tralalero Tralala:
 - Ben
 - Chloe
 - Ella
 - Frank
 - Jade
 - Mason
 - Nina

Tung Tung Tung Tung Tung Tung Tung Tung Tung Sahur:
 - Alice
 - David
 - Hannah

Bombardiro Crocodilo:
 - Grace
 - Isaac
 - Kyle
 - Luna
 - Owen

Random Algorithm Homework 2

import random

# Possible weather types
weather_options = ['Sunny', 'Cloudy', 'Rainy']

# Number of days to simulate
num_days = 7

print("7-Day Weather Forecast:\n")

for day in range(1, num_days + 1):
    weather = random.choice(weather_options)
    print(f"Day {day}: {weather}")

7-Day Weather Forecast:

Day 1: Cloudy
Day 2: Rainy
Day 3: Sunny
Day 4: Rainy
Day 5: Sunny
Day 6: Cloudy
Day 7: Rainy

Simulation Homework

import random

# Number of customers
num_customers = 5

# Generate random service times for each customer (1 to 5 minutes)
service_times = [random.randint(1, 5) for _ in range(num_customers)]

# Calculate total service time
total_time = sum(service_times)

# Print each customer's service time
print("Coffee Shop Queue Simulation:\n")
for i, time in enumerate(service_times, start=1):
    print(f"Customer {i}: {time} minute(s)")

print(f"\nTotal time to serve all customers: {total_time} minutes")

Coffee Shop Queue Simulation:

Customer 1: 2 minute(s)
Customer 2: 2 minute(s)
Customer 3: 5 minute(s)
Customer 4: 5 minute(s)
Customer 5: 5 minute(s)

Total time to serve all customers: 19 minutes