Jeney
Jeney

Reputation: 11

Python program using queue

I want to make a program where a list adds and remove and item using queue however there were problems in my code that needs to be fixed. Please help me, thank you.

class Queue:
    def __init__(self):
        self.items = ["Jene Dayao", "MJ Formaran", "Hans Matias", "Candy Santos", "Ian Domingo"]

    def view(self):
        print (self.items)

    def enqueue(self, item):
        item = input ("Name of the student: ")
        self.items.insert(item)

    def dequeue(self):
        return self.items.pop()
        print ("Student has finished photo taking!")

while True:
    
    print ("School ID Picture Taking Queue")
    print ("Select a function...")
    print ("")
    print ("1. See the student's active list")
    print ("2. A student has finished picture taking")
    print ("3. Add another student to the list")

    option = int(input("Enter your choice: "))

    if option == 1:
        view ()
    elif option == 2:
        enqueue ()
    elif option == 3:
        dequeue ()

Upvotes: 0

Views: 636

Answers (2)

Shreesh Kulkarni
Shreesh Kulkarni

Reputation: 56

The ultimate queue implementation in python

There are a few problems with your code:

  1. you are using the class concept and you are not initializing an object of the class. there are 2 ways of making the code work:

    • initialize an object of the class
    • use only functions and drop using classes altogether.
  2. the enqueue function takes a parameter but you are not passing it. instead, you are taking the value from the user in the function itself. you cannot have it both ways. the preferable solution will be to take input outside the function.

  3. the options are in the wrong order

  4. the enqueue function uses the insert function which takes 2 arguments. instead the append function could be used.

I will be addressing all the issues

The following approach uses classes.

class Queue:
    def __init__(self):
        self.items = ["Jene Dayao", "MJ Formaran",
                      "Hans Matias", "Candy Santos", "Ian Domingo"]

    def view(self):
        print(self.items)

    def enqueue(self, item):
        self.items.append(item)
        print("")

    def dequeue(self):
        print("Student has finished photo taking!")
        return self.items.pop(0)

queue = Queue() # initializing an object of the class Queue

while True:
    print("School ID Picture Taking Queue")
    print("Select a function...")
    print("")
    print("1. See the student's active list")
    print("2. Add another student to the list")
    print("3. A student has finished picture taking")

    option = int(input("Enter your choice: "))

    if option == 1:
        queue.view()
    elif option == 2:
        item = input("Name of the student: ") # taking the new students name
        queue.enqueue(item)
    elif option == 3:
        queue.dequeue()

Upvotes: 1

SpiderPig1297
SpiderPig1297

Reputation: 335

For the next time - try to post the errors you encounter instead of writing "there were problems in my code that needs to be fixed" so that people will know how to help you.

This code has multiple problems:

  1. You try to access functions belongs to the class Queue without ever initializing it. It should be:
    # Must be initialized outside the loop, otherwise you will re-create 
    # it every time, discarding your changes
    queue = Queue()

    while True:

        ...

        option = int(input("Enter your choice: "))

        if option == 1:
            queue.view()
        elif option == 2:
            queue.enqueue()
        elif option == 3:
            queue.dequeue()
  1. In the function enqueue you ask from the user to type the name of the student he wants to add. It is OK, but the problem is that you also expect from the function to receive the item from outside. Remove the item from the function signature, keeping it like the following:
def enqueue(self):
  1. The function input is used to receive a number from the user. To receive a string, which is the type that a student's name should have, use raw_input:
item = raw_input("Name of the student: ")
  1. The line self.items.insert(item) won't work as the function insert should receive the item you want to add, and its index in the list. If you don't care about the index, just use:
self.items.append(item)
  1. You mistakenly switched between options 2 and 3. When a user chooses the 3rd option, you want to call q.enqueue, adding a student to the list. When a user chooses the 2nd option, you want to call q.dequeue, remove the student from the list:
q = Queue()
    if option == 1:
        q.view()
    elif option == 2:
        q.dequeue()
    elif option == 3:
        q.enqueue()

And last thing - it is a good practice to read the documentation of functions you use before using them. Sometimes it may feel that you know how you suppose to call them seeing their name, but as you see its won't always be the case.

Best regards.

Upvotes: 0

Related Questions