John
John

Reputation: 65

Calling a item from a queue data structure list (Python)

I am trying to enqueue 3 people in the list to display the results for each person with all their names at the top but was only able to achieve one result without any names:

Contacting the following  
Phone answered: Yes  
Booked an appointment: No  
Reshedule an appointment again.

I want to make the output to display the all their names and 3 outputs, one for each person from the information stored inside 'names' , and each name to not appear twice.

I want to use a queue to prioritize them according to the list, so I'm trying to put them in order. the if and elif are conditions which will fall into either category depending on the random generator. Now, it is just that the method to include the names inside is not defined.

Code

import random

class Queue:
    def __init__(self):
        self.container = []

    def isEmpty(self):
        return self.size() == 0  

    def enqueue(self, item):
        self.container.append(item)

    def dequeue(self):
        self.container.pop(0)

    def size(self):
        return len(self.container)

    def peek(self) :
        return self.container[0]

names = ["Alvin", "James", "Peter"]

# Enqueuing

q = Queue()
q.enqueue(random.choice(names))

# Dequeuing and Printing
print("Contacting the following:\n" + "\n".join(q.container))  # unsure about this




for i in range(q.size()):

    answered = random.randint(0,1)
    booked = random.randint(0, 1)

    if(answered == 1 and booked == 1):
        print("Now Calling -" + (q.names)) # unsure about this
        print("Phone answered: Yes")
        print("Booked an appointment: Yes")
        print("Booking successful.")

    elif(answered==1 and booked==0):
        print("Now Calling -" + (q.names)) # unsure about this
        print("Phone answered: Yes")
        print("Booked an appointment: No")
        print("Reshedule an appointment again.")

    elif(answered == 0):
        print("Now Calling -" + (q.names)) # unsure about this
        print("Phone answered: No")
        print("Reshedule a callback.")

    q.dequeue()

Example desired output:

Contacting the following
Alvin
James
Peter

Now Calling - James
Phone answered: No
Reshedule a callback.

Upvotes: 2

Views: 709

Answers (1)

PM 2Ring
PM 2Ring

Reputation: 55469

I made a couple of changes to your Queue class. Primarily, the .dequeue method didn't return the item it pops, so it returns the default value of None.

I also changed the .size method to __len__ so you can pass Queue instances to the built-in len function. And gave it an iter method to you can easily use it in for loops, or pass it to .join. I also changed the .isEmpty to .is_empty to conform with Python's PEP-0008 style guide.

Since you want each name to be randomly added to the queue without repetition we don't want random.choice here. Instead we can userandom.shuffle; another option is to use random.sample, although that's more suitable when you want to make a partial selection from a list.

from random import seed, shuffle, randrange

# Seed the randomizer so we can reproduce results while testing
seed(9)

class Queue:
    def __init__(self):
        self.container = []

    def __len__(self):
        return len(self.container)

    def is_empty(self):
        return len(self) == 0

    def enqueue(self, item):
        self.container.append(item)

    def dequeue(self):
        return self.container.pop(0)

    def peek(self) :
        return self.container[0]

    def __iter__(self):
        return iter(self.container)

names = ["Alvin", "James", "Peter"]

# Enqueuing
q = Queue()

# Make a temporary copy of the names that we can 
# shuffle without affecting the original list
temp = names.copy()
shuffle(temp)

# Put the shuffled names onto the queue
for name in temp:
    q.enqueue(name)

# Dequeuing and Printing
print("Contacting the following")
print('\n'.join(q))
#for name in q:
    #print(name)

while not q.is_empty():
    name = q.dequeue()
    print('\nNow Calling -', name)

    answered = randrange(2)
    booked = randrange(2)

    if answered:
        print("Phone answered: Yes")
        if booked:
            print("Booked an appointment: Yes")
            print("Booking successful.")
        else:
            print("Booked an appointment: No")
            print("Reshedule an appointment again.")
    else:
        print("Phone answered: No")
        print("Reshedule a callback.")

output

 Contacting the following
Alvin
Peter
James

Now Calling - Alvin
Phone answered: Yes
Booked an appointment: No
Reshedule an appointment again.

Now Calling - Peter
Phone answered: No
Reshedule a callback.

Now Calling - James
Phone answered: Yes
Booked an appointment: Yes
Booking successful.

In the above code I used

print('\n'.join(q))

to print all of the names, since you use .join for that purpose in your code. But I also showed the alternative way using a simple for loop, but I commented it out:

for name in q:
    print(name)

Upvotes: 2

Related Questions