The "Queue" object has no "size" attribute
I've seen other examples of this event on StackOverflow, but I didn't understand any of the answers (I'm still a new programmer), and the other examples I've seen don't look similar to mine, otherwise I wouldn't "ask this question.
I am running Python 3.2 on Windows 7.
This has never happened to me before, and I have done classes many times, so this time I don't know what it really is. The only difference is that I didn't do the whole class file; I was given a template to fill and a test file to try it out. It was working on a test file but not working on my file. I am calling methods in the class exactly the same as the test file (like 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 type "add" as my decision variable:
line 8, in builtins.AttributeError: "Queue" object has no "size" attribute
So what's going on here? What is different about this?
source to share
Python 3 already has a module queue
(which you can look at). When you do import queue
, Python detects the queue.py
file before it finds your queue.py
.
Rename the file queue.py
to my_queue.py
, change your import statements to from my_queue import Queue
, and your code will work as you intend.
source to share