Я видел другие примеры этого в StackOverflow, но я не понимал ни одного из ответов (я все еще новый программист), и другие примеры, которые я видел, не выглядели так же, как мои, иначе я бы не публиковал это вопрос.
Я использую Python 3.2 в Windows 7.
Со мной такое никогда не случалось раньше, и я много раз проводил занятия таким образом, поэтому я действительно не знаю, что изменилось на этот раз. Единственная разница в том, что я не создал весь файл класса; Мне дали шаблон для заполнения и тестовый файл для примерки. Он работал с тестовым файлом, но не работал с моим. Я вызываю методы класса точно так же, как и тестовый файл (например, Lineup.size ())
Это мой класс:
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)
Это моя программа:
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
И это мое сообщение об ошибке, когда я ввожу «добавить» в качестве переменной решения:
строка 8, во builtins.AttributeError: объект 'Queue' не имеет атрибута 'size'
И так, что здесь происходит? Что в этом особенного?
queue
модуль, а не свой собственный. Попробуйте проверить, что установлено вqueue.__file__
. - person Wessie   schedule 04.02.2013queue
. Переименуйте свойqueue.py
вmy_queue.py
, и ваш код должен работать. - person Blender   schedule 04.02.2013from collections import deque
, который похож на списки, но очень эффективен и выполнит вашу работу. - person TheSHETTY-Paradise   schedule 28.08.2019