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 ()
-
You have a class but you do not create an instance of said class. You're calling instance methods as if they were plain functions. Your enqueue method takes an argument item but you immediately discard that by assigning the variable to the return value of the input call. You're trying to call enqueue with no arguments but the method is defined to expect one. Are you following a tutorial?user5386938– user53869382020年10月02日 12:04:37 +00:00Commented Oct 2, 2020 at 12:04
-
"however there were problems in my code that needs to be fixed." How did you try to use the code? What happened when you did that? How is that different from what is supposed to happen?Karl Knechtel– Karl Knechtel2020年10月02日 12:10:57 +00:00Commented Oct 2, 2020 at 12:10
2 Answers 2
The ultimate queue implementation in python
There are a few problems with your code:
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.
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.
the options are in the wrong order
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()
Comments
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:
- You try to access functions belongs to the class
Queuewithout 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()
- In the function
enqueueyou 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 theitemfrom the function signature, keeping it like the following:
def enqueue(self):
- The function
inputis used to receive a number from the user. To receive astring, which is the type that a student's name should have, useraw_input:
item = raw_input("Name of the student: ")
- The line
self.items.insert(item)won't work as the functioninsertshould 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)
- 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 callq.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.