Hard-to-find technology and rare items are highly sought-after. Imagine that a small shop has asked you to create a queue system to handle its in-store orders for a new, limited item. The shop has accepted sign-ups to the queue by email and has a list of everyone who signed up in chronological order to ensure fairness once the store opens. The shop intends to show the name of the next person in line on the in-store monitors and displays to make sure it’s seen.

In this activity, you’re tasked with designing a queue system that will display the name of each person in the queue in the proper order.
Step 1: Create the Queue

Create a new PythonAnywhere file called queueactivity.py.

We can reuse some of the code from the unit as the base for our queue, but we’ll be using enqueue to add our own data.

The store has a list of 10 people who have signed up to purchase the item, so you’ll need to add their names in chronological order:

Howard Jefferson
Philip Jordan
Margaret Kelly
Irvin Shaw
Loretta Cabrera
Ron Gomez
Cecil Davis
Jack Ludington
Leona Terry
Delbert Bailey

Add comments to your code to describe what you expect each section to do.
Step 2: Create the Display

Next, we want to format our print output to be easily readable by those viewing the screen. Script your output to print this:

There are currently 10 people in line waiting to order.
Attention Howard Jefferson - your order is ready.

1 answer

Certainly! Below is a Python script that models a queue system for the shop. This script includes the names of the people who signed up in chronological order, displays the current queue status, and highlights the next person in line whose order is ready.

# queueactivity.py

class Queue:
    def __init__(self):
        # Initialize an empty list to hold the queue
        self.items = []

    def enqueue(self, item):
        # Add an item to the end of the queue
        self.items.append(item)

    def dequeue(self):
        # Remove and return the item from the front of the queue
        if not self.is_empty():
            return self.items.pop(0)
        else:
            return None

    def is_empty(self):
        # Check if the queue is empty
        return len(self.items) == 0

    def size(self):
        # Return the number of items in the queue
        return len(self.items)

    def peek(self):
        # Return the item at the front of the queue without removing it
        if not self.is_empty():
            return self.items[0]
        else:
            return None

# Step 1: Create the Queue
shop_queue = Queue()

# List of people who signed up to purchase the item
sign_ups = [
    "Howard Jefferson",
    "Philip Jordan",
    "Margaret Kelly",
    "Irvin Shaw",
    "Loretta Cabrera",
    "Ron Gomez",
    "Cecil Davis",
    "Jack Ludington",
    "Leona Terry",
    "Delbert Bailey"
]

# Enqueue each name to the queue
for name in sign_ups:
    shop_queue.enqueue(name)

# Step 2: Create the Display
# Printing the number of people in line
print(f"There are currently {shop_queue.size()} people in line waiting to order.")

# Displaying the next person's order status
next_person = shop_queue.peek()
print(f"Attention {next_person} - your order is ready.")

Explanation of the Code:

  1. Queue Class: This defines a basic queue structure with methods to enqueue (add people), dequeue (remove people), check if the queue is empty, get the size of the queue, and peek at the front of the queue.

  2. Initialization: The shop_queue instance of the Queue class is created.

  3. Sign-ups List: A list of signed-up customers is created in chronological order.

  4. Enqueue Operation: The script iteratively adds each person's name to the queue using the enqueue method in a loop.

  5. Display Output: Finally, the program prints the total number of people in the queue and informs the next customer that their order is ready.

Running the Code

You can save this code to queueactivity.py in PythonAnywhere and run it to see the output as per your requirement.