Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Queue' object has no attribute 'size'

I have seen other examples of this happening on StackOverflow, but I didn't understand any of the answers (I'm still a new programmer,) nor did the other examples I saw look quite like mine, else I wouldn't post this question.

I'm running Python 3.2 on Windows 7.

I have never had this happen to me before and I've done classes this way many times, so I don't really know what is different this time. The only difference is that I didn't make all of the Class file; I was given a template to fill in and a test file to try it on. It worked on the test file, but is not working on my file. I have been calling on the methods in the class in the exact same way as the test file (e.g. Lineup.size())

This is my Class:

class Queue:
    
    # Constructor, which creates a new empty queue:
    def __init__(self):
        self.__items = []
        
    # Adds a new item to the back of the queue, and returns nothing:
    def queue(self, item):
        self.__items.insert(0,item)
        return
        
    # Removes and returns the front-most item in the queue.  
    # Returns nothing if the queue is empty.
    def dequeue(self):
        if len(self.__items) == 0:
            return None
        else:
            return self.__items.pop()
        
    # Returns the front-most item in the queue, and DOES NOT change the queue.  
    def peek(self):
        if len(self.__items) == 0:
            return None
        else:
            return self.__items[(len(self.__items)-1)]
        
    # Returns True if the queue is empty, and False otherwise:
    def is_empty(self):
        return len(self.__items) == 0
    
    # Returns the number of items in the queue:
    def size(self):
        return len(self.__items)
    
    # Removes all items from the queue, and sets the size to 0:
    def clear(self):
        del self.__items[0:len(self.__items)]
        return
        
    # Returns a string representation of the queue:
    def __str__(self):
        return "".join(str(i) for i in self.__items)

This is my program:

from queue import Queue

Lineup = Queue()

while True:
    decision = str(input("Add, Serve, or Exit: ")).lower()
    if decision == "add":
        if Lineup.size() == 3:
            print("There cannot be more than three people in line.")
            continue
        else:
            person = str(input("Enter the name of the person to add: "))
            Lineup.queue(person)
            continue
    elif decision == "serve":
        if Lineup.is_empty() == True:
            print("The lineup is already empty.")
            continue
        else:
            print("%s has been served."%Lineup.peek())
            Lineup.dequeue()
            continue
    elif (decision == "exit") or (decision == "quit"):
        break
    else:
        print("%s is not a valid command.")
        continue

And this is my error message when I enter "add" as my decision variable:

line 8, in builtins.AttributeError: 'Queue' object has no attribute 'size'

So, what is going on here? What is different about this one?

like image 731
Steven Avatar asked Feb 18 '23 15:02

Steven


1 Answers

Python 3 already has a queue module (which you might want to take a look at). When you import queue, Python finds that queue.py file before it finds your queue.py.

Rename your queue.py file to my_queue.py, change your import statements to from my_queue import Queue, and your code will work as you intend.

like image 163
Blender Avatar answered Feb 20 '23 10:02

Blender